JavaScript sorting algorithm (Hill sort, quick sort, merge sort)

Source: Internet
Author: User

Take var a = [4,2,6,3,1,9,5,7,8,0]; for example.



1. Hill sort. The hill sort is an upgrade done in the insert sort above. is a comparison of the distance with a number of methods.


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 an array of currently inserted sort. Console.log (arr);//output This round of sorted array. }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,, 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]

Can be seen by the output. The first round interval is 5. The array of these intervals is inserted in sequence.

Interval is 5:

[4, 2, 6, 3, 1, 9, 5, 7, 8, 0] 4              9    2              5       6              7          3              8             1              0
Sorted by: [4, 2, 6, 3, 0, 9, 5, 7, 8, 1]

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]

Interval is 1:

After sorting:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].


2. Quick Sort. Marks an array with a value in the array. Smaller than this value is placed on the left side of the array, which is worth more than this to the right of the array. And then recursively do the same for the left and right arrays. Until the sort is complete. 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 sort. Combine a series of ordered subsequence sequences into a large complete ordered sequence. Start merging from the smallest units. Then merge the well-ordered arrays gradually. Finally, merge sort is implemented.

Methods for merging 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 sort:

function MergeSort (arr) {var len= arr.length,arrleft=[],arrright =[],gap=1,maxgap=len-1,gaparr=[],glen,n=0;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:2left:6  right:3left:1  right:9left:5  right:7left:8  right:0left:2,4  Right : 3,6left:1,9  right:5,7left:0,8  right:left:2,3,4,6  right:1,5,7,9left:0,8  right:left : 1,2,3,4,5,6,7,9  right:0,8


Look out from the smallest unit to start with.

The first round merges adjacent elements first: 4, 2; 6,3; 1, 9; 5,7; 8,0

When the merge is complete, it becomes: [2,4,3,6,1,9,5,7,0,8]

The second round is combined with 2 elements for one unit: [2,4],[3,6];    [1,9],[5,7]; [0,8],[];

When the merge is complete, it becomes: [2,3,4,6,1,5,7,9,0,8]

The third round is combined with 4 elements for one unit: [2,3,4,6],[1,5,7,9]; [0,8],[]

When the merger is complete, it becomes: [1,2,3,4,5,6,7,9,0,8];

The fourth round is combined with 8 elements for one unit: [1,2,3,4,5,6,7,9],[0,8];

The merge is complete. [0,1,2,3,4,5,6,7,8,9];



Reprint Annotated Source: http://blog.csdn.net/liusaint1992/article/details/51345887




JavaScript sorting algorithm (Hill sort, quick sort, merge sort)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.