Algorithms · Visualized

Comb Sort

Sorting Algorithms

Seventeen sorting algorithms on the same shuffled data — watch comparisons, swaps, pivots and heaps rearrange bars step by step.

Comb Sort improves Bubble Sort by comparing elements that are far apart first (using a shrinking 'gap', similar to Shell Sort), which quickly moves small values ('turtles') out of the back of the array — something plain Bubble Sort is very slow at. Time: O(n² / 2^p), roughly average, O(n²) worst; space: O(1) — sorts in place.

Big-O: Best O(n log n) comparison sorts; counting/radix/bucket reach O(n + k) on small integer keys.

Comb Sort

Sorting

Comb Sort improves Bubble Sort by comparing elements that are far apart first (using a shrinking 'gap', similar to Shell Sort), which quickly moves small values ('turtles') out of the back of the array — something plain Bubble Sort is very slow at. The gap shrinks each pass (commonly divided by 1.3) until it reaches 1, at which point it behaves like Bubble Sort to finish the job.

How Comb Sort Works

  1. Set the initial gap to the length of the array.
  2. Shrink the gap by dividing it by a shrink factor (commonly 1.3), with a minimum gap of 1.
  3. Compare elements that are 'gap' apart and swap them if out of order.
  4. Move through the array with this gap, then shrink the gap again and repeat.
  5. Once the gap is 1, keep doing passes (like Bubble Sort) until a pass with no swaps happens.

Comb Sort Worked Example

Input: [8, 4, 1, 56, 3, -44, 23, -6, 28, 0]

Gap starts at 10, shrinks to 7, then 5, 3, 2, 1 across passes. Large-gap comparisons quickly relocate far-out-of-place values like -44 and -6 toward the front, avoiding the slow 'one step at a time' crawl Bubble Sort would need. Once gap=1, a couple of final cleanup passes finish sorting: [-44,-6,0,1,3,4,8,23,28,56].

Comb SortO(n²) worst, ~O(n log n) avg
Bubble sort with a shrinking gap (÷1.3), killing turtles quickly.
gap7
Step 1 / 64
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Comb Sort — gap = 7
Python · Sorting Algorithms in Code
def comb_sort(a):
    n = len(a)
    gap, shrink = n, 1.3
    while gap > 1:
        gap = max(1, int(gap / shrink))
        for i in range(n - gap):
            if a[i] > a[i + gap]:
                a[i], a[i + gap] = a[i + gap], a[i]
    return bubble_pass(a)

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Comb Sort Time & Space Complexity

BestO(n log n)
AverageO(n² / 2^p), roughly
WorstO(n²)
SpaceO(1) — sorts in place

When to Use Comb Sort

  • A simple drop-in upgrade to Bubble Sort when you want better performance without much added complexity.
  • Teaching the 'shrinking gap' technique before moving on to Shell Sort.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.