Skip to content
StackDecode
SortingAnimated

Comb Sort

Bubble sort with a shrinking gap to kill "turtles".

Time

About O(n²) worst

Extra space

O(1)

Use it for

Simple improvement over bubble sort.

How it works

  1. Bubble sort wastes time because small values at the end move left only one step per pass ("turtles").
  2. Comb sort compares items a gap apart, starting with a gap near n and shrinking it by a factor of 1.3 each round.
  3. When the gap reaches 1 it finishes like bubble sort, but the list is almost sorted by then.

Watch it run

Loading animation…