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
- Bubble sort wastes time because small values at the end move left only one step per pass ("turtles").
- Comb sort compares items a gap apart, starting with a gap near n and shrinking it by a factor of 1.3 each round.
- When the gap reaches 1 it finishes like bubble sort, but the list is almost sorted by then.
Watch it run
Loading animation…