JavaScript Hill sorting, quick sorting, Merge Sorting Algorithm, javascript Hill

Source: Internet
Author: User

JavaScript Hill sorting, quick sorting, Merge Sorting Algorithm, javascript Hill

Take var a = [,]; as an example.

1. sort by hill.Hill sorting is an upgrade made on insert sorting. It is the first method to compare with the distance.

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 array currently sorted by insertion. Console. log (arr); // output the array after this round of sorting. } Gap = parseInt (gap/2);} return arr ;}

Process output:

[4, 9] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [2, 5] "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, 0, 9, 5, 7, 8, 1] [4, 6, 0, 5, 8] "gap:2" [0, 2, 4, 3, 5, 9, 6, 7, 8, 1] [2, 3, 9, 7, 1] "gap:2" [0, 1, 4, 2, 5, 3, 6, 7, 8, 9] [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 first round is 5. Insert and sort the arrays of these intervals in sequence.
The interval is 5:

[4, 9] "gap:5" [4, 2, 6, 3, 1, 9, 5, 7, 8, 0] [2, 5] "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, 0, 9, 5, 7, 8, 1] [4, 6, 0, 5, 8] "gap:2" [0, 2, 4, 3, 5, 9, 6, 7, 8, 1] [2, 3, 9, 7, 1] "gap:2" [0, 1, 4, 2, 5, 3, 6, 7, 8, 9] [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. Fast sorting.Mark an array with a value in the array. Put a value smaller than this value to the left of the array, put a value greater than this value to the right of the array. Then perform the same operation on the array on the left and right recursively. Until the sorting is completed. It is usually marked with the first value of the array.
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. Merge and sort.Combine a series of sorted subsequences into a large complete ordered sequence. Merge from the smallest unit. Then merge the merged ordered arrays. Merge and sort.
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(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 Sorting:

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 [,] 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 

Start with the smallest unit.
In the first round, merge Adjacent Elements in sequence:;
After merging, it becomes: [,]
In the second round, two elements are merged into one unit :[2, 4], []; [], []; [], [];
After merging, it becomes: [,]
In the third round, four elements are merged into one unit :[2, 3, 4, 6], [,]; [], []
After the merge is completed, it becomes: [,];
In the fourth round, 8 elements are merged into one unit:[,], [];
Merge completed. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

The above is all the content of this article, hoping to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.