-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell-sort.js
30 lines (27 loc) · 847 Bytes
/
shell-sort.js
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
function shellSort(arr) {
const moves = [];
var increment = arr.length / 2;
while (increment > 0) {
for (i = increment; i < arr.length; i++) {
var j = i;
var temp = arr[i];
moves.push({indices: [i,j], moveType: "compare"});
while (j >= increment && arr[j-increment] > temp) {
moves.push({indices: [j, j - increment], moveType: "change"});
arr[j] = arr[j-increment];
j = j - increment;
}
moves.push({indices: [j,i], moveType: "change"});
arr[j] = temp;
}
if (increment == 2) {
increment = 1;
} else {
increment = parseInt(increment*5 / 11);
}
}
return moves;
}
function playShellSort() {
playShellPlayInsert(shellSort);
}