-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathShellSort.js
More file actions
31 lines (26 loc) · 1.21 KB
/
Copy pathShellSort.js
File metadata and controls
31 lines (26 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
!function(Sort, undef){
@@USE_STRICT@@
// Comparison Algorithms
// auxilliaries
var gaps = [701, 301, 132, 57, 23, 10, 4, 1], gl = gaps.length;
Sort.ShellSort = function( a ) {
// http://en.wikipedia.org/wiki/Shellsort
// adapted from: https://github.com/mgechev/javascript-algorithms
// Shellsort which uses the gaps in the lexical scope of the IIFE.
var gap, current, k, i, j, N=a.length;
if ( 1 >= N ) return a;
for(k = 0; k < gl; k ++)
{
gap = gaps[k];
for(i = gap; i < N; i += gap)
{
current = a[i];
for(j = i; j >= gap && a[j - gap] > current; j -= gap) a[j] = a[j - gap];
a[j] = current;
}
}
return a;
};
Sort.ShellSort.reference = "http://en.wikipedia.org/wiki/Shellsort";
Sort.ShellSort.description = "Shellsort is an in-place comparison sort. It generalizes an exchanging sort, such as insertion or bubble sort, by starting the comparison and exchange of elements with elements that are far apart before finishing with neighboring elements. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbor exchange.";
}(Sort);