用JS实现插入排序
for(let i=1;i<arr.length;i++){
j=i-1
while(j>=0&&arr[j]>arr[j+1]){
temp = arr[j]
arr[j] = arr[j+1]
arr[j+1] = temp
j--
}
}
用JS实现快排
var quickSort = function(arr) {
if (arr.length <= 1) { return arr; }
var pivotIndex = Math.floor(arr.length / 2);
var pivot = arr.splice(pivotIndex, 1)[0];
var left = [];
var right = [];
for (var i = 0; i < arr.length; i++){
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return quickSort(left).concat([pivot], quickSort(right));
};
用JS实现二分查找法
function search(array,low,high,target){
if(low>high)return -1
let mid = Math.floor((low+high)/2)
if(array[mid]>target){
return search(array,low,mid-1,target)}
if(array[mid]<target){
return search(array,mid+1,high,target)}
return mid
}