Comb Sort: Mechanism, Complexity Analysis & Interactive Visualizer
Comb Sort
An ingenious improvement on Bubble Sort: compares elements at a shrinking gap size to eliminate small values trapped near the end of the array ("turtles") before finishing with a final bubble pass.
How it works
Comb Sort, designed by Włodzimierz Dobosiewicz in 1980 and popularized by Stephen Lacey and Richard Box in 1991, addresses the core inefficiency of Bubble Sort: "turtles". In Bubble Sort, large values at the beginning move quickly ("rabbits"), but small values at the end move left very slowly ("turtles"). Comb Sort fixes this by comparing elements separated by a gap:
- Initial Gap: Start with a gap equal to the array length \(n\).
- Shrink Stage: On each pass, divide the gap by a shrink factor (proven mathematically to be optimal at \(\approx 1.3\)). If the gap drops below 1, set it to 1.
- Compare and Swap: Traverse the array comparing elements at index \(i\) and \(i + \text{gap}\). Swap them if they are out of order.
- Termination: Repeat the shrinking passes until the gap is 1 and a complete pass is made with zero swaps (reverting to a standard bubble sort checks).
Complexity
Interactive visualization
Pseudocode
procedure combSort(A): n ← length(A) gap ← n shrink ← 1.3 sorted ← false while not sorted: gap ← floor(gap / shrink) if gap ≤ 1: gap ← 1 sorted ← true # assume sorted for the final bubble sort sweep i ← 0 while i + gap < n: if A[i] > A[i + gap]: swap A[i], A[i + gap] sorted ← false i ← i + 1
JavaScript implementation
function combSort(arr) { const n = arr.length; let gap = n; const shrink = 1.3; let sorted = false; while (!sorted) { gap = Math.floor(gap / shrink); if (gap <= 1) { gap = 1; sorted = true; } let i = 0; while (i + gap < n) { if (arr[i] > arr[i + gap]) { [arr[i], arr[i + gap]] = [arr[i + gap], arr[i]]; sorted = false; } i++; } } return arr; }
Pros and cons
Pros
- Extremely easy to implement and verify.
- Requires O(1) auxiliary memory (fully in-place).
- Highly effective at scattering "turtles" early on.
- Performs much faster than traditional Bubble Sort.
Cons
- Unstable sorting order.
- Worst-case is still O(n²) under highly specific adversarial layouts.
- Slower than Quicksort or Mergesort on large datasets.
When (not) to use it
- Replacing simple sorts: Excellent if you need a quick sorting routine that is faster than Bubble Sort or Insertion Sort but requires zero complex stack/heap setups.
- Strictly stable lists: Do not use if duplicate order preservation is required.
Related reading
Sleep Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A unique concurrency-based sorting algorithm: spawns a separate thread for each element, sleeping for a duration proportional to the element value before writing it to the output.
By Md. Harun-Or-Roshid
Cube Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A parallel dimension sorting algorithm: organizes elements in a 3D coordinate cube and sorts vectors along the height, width, and depth axes sequentially to achieve sorted order.
By Md. Harun-Or-Roshid
Patience Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A card-game inspired sorting algorithm: distributes elements onto a series of piles such that each pile's top is \(\ge\) the incoming value, then performs a multi-way merge across pile tops.
By Md. Harun-Or-Roshid
Library Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A gapped variation of Insertion Sort: leaves empty slots (gaps) in the array, allowing elements to be inserted in \(O(1)\) shift time on average by avoiding massive shifts.
By Md. Harun-Or-Roshid