<>1, Recursive sorting method ( Low performance consumption ):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document
</title> </head> <body> <script> let arr = [1,4,2,6,7,9,11,5,8,10,3]; //let arr
= [1,4,2,3]; // You can understand it better with this demo // divide function mergeSort(arr){ let len = arr.length;
// At first, compare two if(len < 2){ return arr; } // with mid As a dividing line let mid = Math.floor(len/2)
; // Split into left to right Two arrays let left = arr.slice(0,mid); let right = arr.slice(mid);
// recursion Reverse thinking for the last time mergeSort(left) to mergeSort(right) All lengths are 1 The situation of return merge(mergeSort(
left),mergeSort(right)); } // sort function merge(left,right){ let result = [];
while(left.length >0 && right.length >0){ if(left[0] <= right[0]){
// delete left First item of array push reach result within result.push(left.shift()); }else{ result.push(right.
shift()); } } // At this point, if there is a value in another array, it will be deleted push reach result In the array while(left.length){ result.push(
left.shift()); } while(right.length){ result.push(right.shift()); } // Returns a sorted array
return result; } let newArr = mergeSort(arr); console.log(newArr); //[1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11] </script> </body> </html>
<>2, Quick sort method :
// Quick sort let arr = [1,4,2,6,7,9,11,5,8,10,3]; function quickSort(arr){ let len =
arr.length; quick(arr,0,len-1); } function quick(arr,left,right){ let point =
left; // First item let index = point + 1; // Traversal starting point let len = arr.length;
// with arr[point] As a reference Start from the beginning and traverse to the end Will be better than arr[point] Small values on the left for(let i=index; i<=right; i++){
if(arr[i] < arr[point]){ swap(arr,i,index); // Change position index++; // Move the swap point forward one bit } } // Finally, let
First item And arr[index-1] In exchange swap(arr,point,index-1); index--; //arr Left and right split points let leftE =
index-1; //left The end of let rightB = index+1; //right The starting point of // Each side is greater than 2 Recursion is only needed for the number of nodes Until there is only one number on each side if(
leftE> left){ quick(arr,point,leftE); } if(rightB < right){ quick(arr,rightB,
right); } } // Change position function swap(arr,i,j){ let tmp = arr[i]; arr[i] = arr[j]; arr
[j] = tmp; } quickSort(arr) console.log(arr) //[1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11]
<>3, Heap sort method :
let arr = [1,4,2,6,7,9,11,5,8,10,3]; // Heap sort function heapSort(arr){ let len =
arr.length; // Initialization heap adjustment for(let j=Math.floor(len/2)-1; j>=0; j--){ heap(arr,j,len);
} for(let i =len-1; i>0; i--){ swap(arr,0,i); len --; heap(arr,0,len); } }
function heap(arr,i,len){ // From the first i One start, one layer at a time, reverse to get the maximum number let left = 2*i+1, // Get the left subset right
= 2*i+2; // Get the right subset max = arr[i], // Let the last one be the maximum maxIndex = i; // Compare with the maximum value And update the maximum value max
Index if(left<len && arr[left]>max){ maxIndex = left; } if(right<len && arr[
right]>max){ maxIndex = right; } if(maxIndex !== i){ // Maximum exchange swap(arr,i,maxIndex
); heap(arr,maxIndex,len); } } // exchange function swap(arr,i,j){ let tmp = arr[i];
arr[i] = arr[j]; arr[j] = tmp; } heapSort(arr) console.log(arr) //[1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11]
<>4, Radix sort : ( Low consumption )

// Radix sort function redixSort(arr){ let len = arr.length; // Find the largest number of digits in the array let maxNum
= arr[0]; let redixArr = []; for(let i=1; i<len; i++){ if(arr[i] > maxNum){
maxNum= arr[i]; } } let x=10,y=1; let maxDigit = maxNum.toString().length;
// Maximum number of digits for(let i=0; i<maxDigit; i++){ for(let j=0; j<len; j++){ let v = Math.
floor(arr[j] % x/y); if(!redixArr[v])redixArr[v] = []; redixArr[v].push(arr[j]);
// Individual position Ten Hundred ... sort } x *=10; y *=10; let index =0; // Sorted traversal loops arr In array for(let j=0,len=
redixArr.length;j<len; j++){ let item = redixArr[j]; if(!item)continue; item.
forEach(v=>{ arr[index ++] = v; }); } // In the end redixArr Sort empty waiting for next sort redixArr = []; }
} arr = [233,434,565,23,78,2,54,56,9]; redixSort(arr); console.log(arr); //[2,
9, 23, 54, 56, 78, 233, 434, 565]

Technology