Shell Sort Visualization
Speed:
Step: 0 / -1Algorithm Code
1function shellSort(arr) {
2 const n = arr.length;
3
4 // Start with a large gap, then reduce the gap
5 // Common gap sequence: n/2, n/4, n/8, ..., 1
6 for (let gap = Math.floor(n/2); gap > 0; gap = Math.floor(gap/2)) {
7
8 // Perform insertion sort for this gap
9 for (let i = gap; i < n; i++) {
10 // Save arr[i] in temp and make a hole at position i
11 let temp = arr[i];
12
13 // Shift elements until the correct location for arr[i] is found
14 let j;
15 for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
16 arr[j] = arr[j - gap];
17 }
18
19 // Put temp in its correct location
20 arr[j] = temp;
21 }
22 }
23
24 return arr;
25 }Visualization