This is the only sorting algorithm whose complexity NOBODY has fully solved.
#dsa #shellsort #sortingalgorithms
This is the only sorting algorithm whose complexity NOBODY has fully solved.
Meet Shell sort.
It’s just insertion sort with ONE idea added: let elements jump.
Insertion sort is slow because it only swaps neighbors. A small element stuck on the right crawls left one step at a time. Painful.
Shell sort’s fix: sort elements a “gap” apart first.
→ Gap 4: compare every 4th element, swap
→ Gap 2: getting closer to sorted
→ Gap 1: plain insertion sort - but now it’s FAST, because the array is almost sorted already
Big jumps first. Small steps last.
Same 8-element array as the series:
Insertion: 20 compares, 15 shifts
Shell (4,2,1): 18 compares, 9 shifts
Fewer shifts - the early passes already moved everything most of the way home.
Three things worth knowing:
1. Its Big-O depends on the GAPS you pick, and nobody’s solved it. Shell’s gaps → O(n²). Hibbard’s → O(n^1.5). Sedgewick’s → O(n^4/3). No gap sequence is proven optimal. It’s the rare textbook algorithm whose complexity is still an open question.
2. It’s the counterexample to “always use O(n log n).” Worse than merge sort on paper, but it ships in real embedded systems - uClibc, firmware, tight-memory code. No recursion, no extra memory, tiny footprint. When you have 2KB of RAM, “fits and runs” beats “asymptotically optimal.”
3. It broke the O(n²) barrier in 1959. Before Shell, everyone assumed in-place sorting meant swapping neighbors, and n² was the price. Shell proved a sub-quadratic in-place sort existed at all.
The real lesson: a tiny change to a “solved” problem - let elements jump instead of shuffle - can reopen it completely.
Save this before your next interview.
Full breakdowns → algoinsight.io
Stop Grinding, Start Getting Offers.
#dsa #shellsort #sortingalgorithms #codinginterview #leetcode
Comments
Comments from the original post on Instagram
No comments yet
