First, what is the sort Hill
Hill sorting (Shell's Sort) is
inserted into the sort
one called "narrow increment sort," An Algorithm is inserted directly into a more efficient improved version.
Ideas:
Shell sort is the subject of pressing the recording packet in increments, the use of direct insertion sort each sorting algorithm; With increment is gradually reduced, more and more keywords each comprising, when reduced to an increment, the entire file is divided into just one set, the algorithm is terminated
logic:
In the Shell sort is the most important group, we first find a gap, at regular intervals these numbers qualifying group,
Consider a array [9,1,2,5,7,4,8,6,3,5]; about every 5 as a group. This array is then we divided into the following groups
Group I: 94
Group II: 18
Group III: 26
Group IV: 53
The fifth group: 75
After the packet is completed we sort of set in every group
Group I: 49
Group II: 18
The third set of 26
The fourth group 35
The fifth group 57
We sorted array will redesign the reordering interval (the time interval is halved) for a group of 2
[4,1,2,3,5,9,8,6,5,7]
First set: 42585 == "the ordered set 24558
Second set: 13967 == "the ordered set 13679
2 1 4 3 5 6 5 7 8 9
After ordering we will redesign the array interval reorder (next interval halved) one last time for a group that is pairwise comparisons
1 2 3 4 5 5 6 7 8 9
Note: Hill ordering interval is generally not particularly defined, usually half the length of the array
Second, the code
var arr = [2,5,1,9,0] //算间隔 var len = Math.floor(arr.length/2) while(len>0){ for(var i=len;i<arr.length;i++){ var temp = arr[i]; for(var j=i-len;j>=0&&temp<arr[j];j=j-len){ arr[j+len] = arr[j] } arr[j+len] = temp; } len = Math.floor(len/2) }