Skip to content

Commit bfffaaf

Browse files
committed
Shellsort added
1 parent add5397 commit bfffaaf

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

src/sorting/shellsort.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Shellsort
3+
*
4+
* Shellsort uses the gaps 701, 301, 132, 57, 23, 10, 4, 1 and uses insertion sort
5+
* to sort the sub-arrays which match for the different gaps.
6+
*/
7+
var shellsort = (function () {
8+
9+
var gaps = [701, 301, 132, 57, 23, 10, 4, 1];
10+
11+
/**
12+
* Shellsort which uses the gaps in the lexical scope of the IIFE.
13+
*
14+
* @public
15+
* @param {array} array Array which should be sorted
16+
* @return {array} Sorted array
17+
*/
18+
return function (array) {
19+
var gap, current;
20+
21+
for (var k = 0; k < gaps.length; k += 1) {
22+
gap = gaps[k];
23+
for (var i = gap; i < array.length; i += gap) {
24+
current = array[i];
25+
for (var j = i; j >= gap && array[j - gap] > current; j -= gap) {
26+
array[j] = array[j - gap];
27+
}
28+
array[j] = current;
29+
}
30+
}
31+
return array;
32+
};
33+
34+
}());

0 commit comments

Comments
 (0)