forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.js
More file actions
27 lines (21 loc) · 688 Bytes
/
ShellSort.js
File metadata and controls
27 lines (21 loc) · 688 Bytes
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
function shellSort(unsortedArray, start, end) {
let gap = 1;
while (gap < end) {
gap = gap * 3 + 1;
}
gap = parseInt(gap / 3);
let j;
while (gap > 0) {
for (let i = gap; i < end; i += 1) {
j = i;
while (j >= gap && unsortedArray[j - gap] > unsortedArray[j]) {
[unsortedArray[j - gap], unsortedArray[j]] = [unsortedArray[j], unsortedArray[j - gap]];
}
}
gap = gap / 2;
}
return unsortedArray;
}
var unsortedArray = [54, 42, 11, 33, 24, 99, 77, 80];
let sortedArrayViaShellSort = shellSort(unsortedArray, 0, unsortedArray.length);
console.log(sortedArrayViaShellSort);