AiTechWorlds
AiTechWorlds
Visualize Shell Sort using shrinking gap sequences, an in-place improvement over Insertion Sort, with synced code.
def shell_sort(a):
n = len(a)
gap = n // 2
while gap > 0:
for i in range(gap, n):
key, j = a[i], i
while j >= gap and a[j-gap] > key:
a[j] = a[j-gap]; j -= gap
a[j] = key
gap //= 2Shell Sort generalizes Insertion Sort by comparing elements a gap apart, shrinking the gap each round so elements move long distances early and finish nearly sorted.
| Best | O(n log n) |
| Average | O(n^1.25) |
| Worst | O(n²) |
| Space | O(1) |
| Stable | No |
| In-place | Yes |
The Shell Sort Visualizer animates how Shell Sort works: it generalizes Insertion Sort by comparing and shifting elements that are a 'gap' apart, then shrinks the gap each round until it becomes 1. This lets elements move long distances early, so the array is nearly sorted by the final pass. Shell Sort is in-place, uses O(1) extra space, and is not stable.
Generate an array
Click New Array or set the size slider for a random dataset.
Play the animation
Press Play to watch gapped comparisons and shifts as the gap shrinks.
Step and scrub
Step forward/back or drag the timeline to inspect each gap pass.
Read the synced code
The highlighted pseudocode line follows the gap loop; switch tabs for C++, Java, Python, or JavaScript.
100% Private — No Server Required
All processing happens directly in your browser. No data is uploaded, stored, or transmitted to any server.
Last reviewed on June 14, 2026 by the AiTechWorlds Tools Team. All processing runs locally in your browser.