Take var a = [4,2,6,3,1,9,5,7,8,0]; as an example.
1. Hill sort. Hill sorting is an upgrade made on insert sorting. It is some methods to compare with those with those with a distance first.
function shellsort(arr){ var i,k,j,len=arr.length,gap = Math.ceil(len/2),temp; while(gap>0){ for (var k = 0; k < gap; k++) { var tagArr = []; tagArr.push(arr[k]) for (i = k+gap; i < len; i=i+gap) { temp = arr[i]; tagArr.push(temp); for (j=i-gap; j >-1; j=j-gap) { if(arr[j]>temp){ arr[j+gap] = arr[j]; }else{ break; } } arr[j+gap] = temp; } console.log(tagArr,"gap:"+gap);//Output the currently inserted sorted array. console.log(arr);//Output the array after this round of sorting. } gap = parseInt(gap/2); } return arr; }Process output:
[4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [6, 7] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [3, 8] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 6, 3, 0, 9, 5, 7, 8, 0] [6, 7] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 6, 3, 0, 9, 5, 7, 8, 0] [6, 7] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 6, 3, 0, 9, 5, 7, 8, 0] [6, 7] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 3, 5, 9, 6, 7, 8, 1] [2, 3, 9, 7, 1] "gap:2" [0, 1, 4, 2, 5, 3, 6, 7, 8, 9] "gap:1" [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
It can be seen from the output. The interval between the first round is 5. Sort the arrays of these intervals in turn.
The interval is 5:
[4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [6, 7] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [3, 8] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 6, 3, 0, 9, 5, 7, 8, 0] [6, 7] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 6, 3, 0, 9, 5, 7, 8, 0] [6, 7] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 6, 3, 0, 9, 5, 7, 8, 0] [6, 7] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [1, 0] "gap:5" [4, 2, 3, 5, 9, 6, 7, 8, 1] [2, 3, 9, 7, 1] "gap:2" [0, 1, 4, 2, 5, 3, 6, 7, 8, 9] "gap:1" [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
The interval is 2:
[4, 2, 6, 3, 0, 9, 5, 7, 8, 1] 4 6 0 5 8 2 3 9 7 1
After sorting:
[0, 1, 4, 2, 5, 3, 6, 7, 8, 9]
The interval is 1:
After sorting:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].
2. Quick sort. Make an array marked with a value in the array. Put the value smaller than this to the left of the array, and put the value larger than this to the right of the array. Then recursively perform the same operation on the left and right arrays. Until the sorting is complete. Usually the first value of the array is marked.
Code:
function quickSort(arr){ var len = arr.length,leftArr=[],rightArr=[],tag; if(len<2){ return arr; } tag = arr[0]; for(i=1;i<len;i++){ if(arr[i]<=tag){ leftArr.push(arr[i]) }else{ rightArr.push(arr[i]); } } return quickSort(leftArr).concat(tag,quickSort(rightArr)); }3. Order and sort. Merge a series of sorted subsequences into a large, complete ordered sequence. Merge starting with the smallest unit. Then gradually merge the merged ordered arrays. Finally, merge sorting is achieved.
Methods to merge two ordered arrays:
function subSort(arr1,arr2){ var len1 = arr1.length,len2 = arr2.length,i=0,j=0,arr3=[],bArr1 = arr1.slice(),bArr2 = arr2.slice(); while(bArr1.length!=0 || bArr2.length!=0){ if(bArr1.length == 0){ arr3 = arr3.concat(bArr2); bArr2.length = 0; }else if(bArr2.length == 0){ arr3 = arr3.concat(bArr2); bArr2.length = 0; }else if(bArr2.length == 0){ arr3 = arr3.concat(bArr1); bArr1.length = 0; }else{ if(bArr1[0]<=bArr2[0]){ arr3.push(bArr1[0]); bArr1.shift(); }else{ arr3.push(bArr2[0]); bArr2.shift(); } } return arr3; }Merge sort:
function mergeSort(arr){ var len= arr.length,arrleft=[],arrright =[],gap=1,maxgap=len-1,gapArr=[],glen,n; while(gap<maxgap){ gap = Math.pow(2,n); if(gap<=maxgap){ gapArr.push(gap); } n++; } glen = gapArr.length; for (var i = 0; i < glen; i++) { gap = gapArr[i]; for (var j = 0; j < len; j=j+gap*2) { arrleft = arr.slice(j, j+gap); arrright = arr.slice(j+gap,j+gap*2); console.log("left:"+arrleft,"right:"+arrright); arr = arr.slice(0,j).concat(subSort(arrleft,arrright),arr.slice(j+gap*2)); } } return arr; }Sort [4,2,6,3,1,9,5,7,8,0] output:
left:4 right:2 left:6 right:3 left:1 right:9 left:5 right:7 left:8 right:0 left:2,4 right:3,6 left:1,9 right:5,7 left:0,8 right: left:2,3,4,6 right:1,5,7,9 left:0,8 right: left:1,2,3,4,5,6,7,9 right:0,8
It can be seen that starting from the smallest unit.
The first round first merges adjacent elements in sequence: 4,2; 6,3; 1,9; 5,7; 8,0
After the merge is completed, it becomes: [2,4,3,6,1,9,5,7,0,8]
The second round merges in one unit of 2 elements: [ 2,4],[3,6]; [1,9],[5,7]; [0,8],[];
After the merge is completed, it becomes: [2,3,4,6,1,5,7,9,0,8]
The third round merges in one unit of 4 elements: [ 2,3,4,6],[1,5,7,9]; [0,8],[]
After the merge is completed, it becomes: [1,2,3,4,5,6,7,9,0,8];
The fourth round merges in one unit of 8 elements: [1,2,3,4,5,6,7,9],[0,8];
Merge is completed. [0,1,2,3,4,5,6,7,8,9];
The above is all about this article, I hope it will be helpful to everyone's learning.