const Compare = { LESS_THAN:0, BIGGER_THAN:1, EQUAL:2, } function defaultCompare(a,b){ return a === b?Compare.EQUAL:a>b?Compare.BIGGER_THAN:Compare.LESS_THAN; } function swap(array,i,j){ let tmp = array[i]; array[i] = array[j]; array[j] = tmp; } let arr1 = [5,6,4,2,1,4,4,4241,224,99,66666]; //快速排序 function quickSort(array,compareFn = defaultCompare){ return quick(array,0,array.length-1,compareFn); } function quick(array,left,right,compareFn){ if(array.length > 1){ let index = partition(array,left,right,compareFn); if(left < index - 1){ quick(array,left,index - 1,compareFn); } if(right > index){ quick(array,index,right,compareFn); } } return array; } function partition(array,left,right,compareFn){ let i = left; let j = right; let pivot = array[Math.floor((right + left) / 2)]; while(i <= j){ while(compareFn(array[i],pivot) === Compare.LESS_THAN){ i++; } while(compareFn(array[j],pivot) === Compare.BIGGER_THAN){ j--; } if(i <= j){ swap(array,i,j); i++; j--; } } return i; } console.log(quickSort(arr1)); //插入排序 function insertSort(array,compareFn = defaultCompare){ for(let i = 0; i < array.length; i++){ for(let j =i+1; j >0; j--){ if(compareFn(array[j-1],array[j]) === Compare.BIGGER_THAN){ swap(array,j,j-1); } } } return array; } console.log(insertSort(arr1)); //归并排序 function mergeSort(array,compareFn = defaultCompare){ if(array.length > 1){ const {length } = array; const middle = Math.floor(length / 2); const left = mergeSort(array.slice(0,middle)); const right = mergeSort(array.slice(middle,length)); array = merge(left,right,compareFn); } return array; } function merge(left,right,compareFn){ let i = 0; let j = 0; const result = []; while(i < left.length && j < right.length){ result.push( compareFn(left[i],right[j]) ===Compare.LESS_THAN ? left[i++]:right[j++] ) } return result.concat( i < left.length ? left.slice(i):right.slice(j) ) } console.log(mergeSort(arr1)); //希尔排序
function shellSort(array,compareFn = defaultCompare){
let d = array.length;
while(d>1){
d = Math.ceil(d/2);
for(let x = 0; x < d; x++){
for(let i = x+d; i < array.length; i +=d){
for(let j = i - d; j >= 0 ; j -= d){
if(compareFn(array[j], array[i]) === Compare.BIGGER_THAN){
swap(array,j,i);
break;
}
}
}
}
}
return array;
}
console.log(shellSort(arr1)); //堆排序 function heapSort(array,compareFn = defaultCompare){ let heapSize = array.length; buildMaxHeap(array,compareFn); while(heapSize>1){ swap(array,0,--heapSize); heapIfy(0,heapSize,array,compareFn); } return array; } function buildMaxHeap(array,compareFn){ for(let i = Math.floor(array.length / 2); i >= 0; i--){ heapIfy(i,array.length,array,compareFn); } return array; } function heapIfy(index,length,array,compareFn){ let element = index; let left = index*2+1; let right = index*2+2; if(left < length && compareFn(array[left],array[element]) === Compare.BIGGER_THAN){ element = left; } if(right < length && compareFn(array[right],array[element]) === Compare.BIGGER_THAN){ element = right; } if(element != index){ swap(array,element,index); heapIfy(element,length,array,compareFn); } } console.log(heapSort(arr1));