Core idea: First determine an element interval number gap, and then the sequence of participating in the order of this interval from the beginning of the 1th element into a number of sub-sequences, that is, all positions spaced gap is considered as a sub-sequence, in each sub-sequence with some sort of sorting method to sort; then decrease the number of intervals. and re-divides the whole sequence into several sub-sequences according to the new interval number, then sorts each subsequence separately, so that it goes on until the interval number gap=1;
var arr = [38,49,65,97,76,13,27,49];
Shellsort (arr);
Console.log (arr);
varShellsort =function(arr) {varI, j,temp; varn =arr.length; varGap =N; while(gap>1) {Gap= GAP/2; Do{flag= 0; for(i=0;i<n-gap;i++) {J= i+Gap; if(arr[i]>Arr[j]) {Temp=Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; Flag= 1; } } } while(flag!=0); }}
Algorithm analysis: The time complexity of the shell sort is slightly greater than O (nlog2n) near O (n1.23), unstable sort, not suitable for linked lists.
Shell sort (reduce incremental sort)