折半查找算法
var data = [3, 12];
var goal = 3;
function bSearch(goal, array, indexStart, indexEnd) {
if (indexStart > indexEnd) {
return -1;
}
var mid = indexStart + Math.floor((indexEnd - indexStart)/2, 10);
if (array[mid] === goal) {
return mid;
}
if (goal > array[mid]) {
indexStart = mid + 1;
}
else if (array[mid] > goal) {
indexEnd = mid - 1;
}
return bSearch(goal, array, indexStart, indexEnd);
}
console.log(bSearch(goal, data, 0, data.length - 1));
快速排序
Array.prototype.quickSort = function () {
var arr = this || [];
if (arr.length <= 1) {
return arr;
}
var goalIndex = Math.floor(arr.length / 2);
var goal = arr[goalIndex];
var leftArr = [];
var rightArr = [];
arr.forEach(function(val, index){
if(goalIndex === index) {
return;
}
if (val > goal) {
rightArr.push(val);
}
else {
leftArr.push(val);
}
});
return leftArr.quickSort().concat(goal, rightArr.quickSort());
}
var arr = [5, 2, 3, 4, 5];
console.log(arr.quickSort());
按多健值排序
var by = function () {
var args = arguments || [];
return function (a, b) {
var key = '';
for (var i = 0; i < args.length; i ++) {
key = args[i];
if(a[key] === b[key]) {
if (i === args.length - 1) {
return 0;
}
continue;
}
return a[key] < b[key] ? -1 : 1;
}
}
}
var obj = [
{id: 3, name: 'b'},
{id: 1, name: 'a'},
{id: 2, name: 'b'},
{id: 1, name: 'c'}
]
console.log(obj.sort(by('name', 'id')));