Comb Sort
- Ω( n log n )
- Θ( n2 )
- O( n2 )
- space O( 1 )
Comb sort is bubble sort with a gap. Instead of looking only at neighbours, each pass compares elements a fixed distance apart. The distance starts at the array length, shrinks by a factor of 1.3 every pass, and ends at 1, where the pass is plain bubble sort. Long-range swaps move small values toward the front early, so the closing passes find few inversions left.
- Comparisons count only element-against-element checks; loop and index checks are never counted.
- Reads and writes include reads and writes to helper buffers (merge, counting, radix, bucket and tim sort).
- A swap counts once, as a swap. It is never also counted as a read or a write.
- Real time is the average time of the counted sort on this device over repeated runs, measured once before playback and independent of animation speed. Visual time is the time spent animating, excluding pauses.
How comb sort works
For six elements the gaps are 4, 3, 2 and 1, since the code divides by 1.3 and rounds down each time: 6 becomes 4, 4 becomes 3, 3 becomes 2 and 2 becomes 1. With gap 4 the pass compares index 0 with 4 and index 1 with 5. The first pair holds 5 and 4, which are out of order, so they swap and the array becomes [4, 2, 6, 1, 5, 3]. The pair 2 and 3 is fine.
Gap 3 compares indexes 0 and 3, 1 and 4, 2 and 5. The 4 stands before the 1, so those swap to give [1, 2, 6, 4, 5, 3]. The pair 2 and 5 is in order. Then 6 and 3 swap, and the array reads [1, 2, 3, 4, 5, 6]. Three swaps were enough to sort an array that holds nine inversions, because a swap across a distance can cancel several inversions at once. Bubble sort needs one swap per inversion.
Nothing more changes, but the algorithm only learns that by finishing its schedule. The gap 2 pass makes four comparisons with no swap, and the gap 1 pass makes five comparisons with no swap. Only a pass at gap 1 that swaps nothing is allowed to end the loop. The final count is 14 comparisons and 3 swaps.
| Step | Change | Array after |
|---|---|---|
| 1 | Swap indexes 0 and 4 | 4 2 6 1 5 3 |
| 2 | Swap indexes 0 and 3 | 1 2 6 4 5 3 |
| 3 | Swap indexes 2 and 5 | 1 2 3 4 5 6 |
All 3 changes are shown.
Pseudocode
gap = n
sorted = false
while not sorted
gap = max(1, floor(gap / 1.3))
sorted = (gap == 1)
for i = 0 to n - gap - 1
if a[i + gap] < a[i]
swap a[i], a[i + gap]
sorted = false
Why these complexities
The cost per pass is n minus the gap comparisons, and the number of passes is the number of times 1.3 fits into n, roughly 3.8 times the natural logarithm of n. For 1000 elements this code runs 22 gap values, 769, 591, 454 and so on down to 1. A pass happens whatever the data looks like, so even an array that is already sorted costs 18,713 comparisons at n = 1000, against 999 for bubble sort. That is the n log n best case, and it is why comb sort is not adaptive.
The worst case is quadratic. The gap schedule is fixed and independent of the data, so an input can be built where the large-gap passes leave many inversions behind and the final gap 1 passes have to remove them one adjacent swap at a time, like bubble sort. How the average case behaves is less well pinned down by proof. Measurements on random data stay close to n log n rather than n², and that is why the method is popular in spite of its quadratic bound, which is the figure this page lists.
Space is constant: the gap, a flag and an index. Stability is lost. With three elements the gap is 2, and a pass that swaps index 0 with index 2 can carry an element past an equal one that sat between them, so the order of equal keys is not preserved. The loop condition keeps running while the gap exceeds 1 or the last pass swapped, which guarantees the final pass at gap 1 is a clean one before the loop ends.
Measured on this site
Exact counts from this site's own engine for 32 elements, using the same rules as the visualiser.
| Input | Comparisons | Writes | Swaps |
|---|---|---|---|
| Random | 236 | 0 | 59 |
| Already sorted | 205 | 0 | 0 |
| Reversed | 236 | 0 | 28 |
| Few distinct values | 236 | 0 | 22 |
When to use it
Use it when you want something barely more complex than bubble sort but much faster in practice, and you do not need stability. It needs no extra memory and no recursion.
Frequently asked questions
Why shrink the gap by 1.3?
The factor was found by experiment. Much smaller values make the algorithm slower, and much larger ones leave too many inversions for the final passes.
Is comb sort stable?
No. Swapping elements that are far apart can move an element past an equal one.
What is its worst case?
O(n²), reached only for specially built inputs. On typical data it behaves close to O(n log n).
Which gaps does this comb sort use?
It starts from the array length, and every pass sets the new gap to the old one divided by 1.3 and rounded down, never below 1. For 6 elements that gives 4, 3, 2, 1. For 1000 elements it gives 769, 591, 454, 349, 268, 206, 158, 121, 93, 71, 54, 41, 31, 23, 17, 13, 10, 7, 5, 3, 2, 1, which is 22 passes before the run can finish.
How does comb sort differ from Shell sort?
Both start with a large gap and shrink it, but Shell sort fully sorts the interleaved subsequences at each gap with insertion sort, so a gap is done after one round. Comb sort makes just one bubble-style pass per gap and moves on, leaving the cleanup to the shorter gaps, and the final gap 1 phase repeats until a pass swaps nothing. The pass is simpler, and the price is no guaranteed bound better than quadratic.
Background
Wlodzimierz Dobosiewicz published the idea in 1980 in a paper on an efficient variation of bubble sort. It was rediscovered in 1991 by Stephen Lacey and Richard Box, who described it in BYTE, gave it the name comb sort and tuned the shrink factor of about 1.3 by experiment on many random lists.
The method does for bubble sort what Shell's method does for insertion sort: it uses large gaps first to reduce disorder cheaply. A variant, often called comb sort 11, jumps the gap to 11 whenever it would become 9 or 10, because those two values left more disorder behind for the final passes. This implementation does not use that rule and follows the plain 1.3 schedule.
References
- Dobosiewicz, An Efficient Variation of Bubble Sort, Information Processing Letters 11(1), 1980
- Lacey and Box, A Fast, Easy Sort, BYTE, April 1991
- Knuth, The Art of Computer Programming, vol. 3, section 5.2.1 (Sorting by Insertion, Shell's method)
Compare with
| Algorithm | Average time | Worst time |
|---|---|---|
| Bubble Sort | Θ( n2 ) | O( n2 ) |
| Cocktail Shaker Sort | Θ( n2 ) | O( n2 ) |
| Gnome Sort | Θ( n2 ) | O( n2 ) |
| Odd-Even Sort | Θ( n2 ) | O( n2 ) |
| Quick Sort | Θ( n log n ) | O( n2 ) |
| 3-Way Quick Sort | Θ( n log n ) | O( n2 ) |
| Dual-Pivot Quick Sort | Θ( n log n ) | O( n2 ) |
| Best time | Ω( n log n ) | good |
|---|---|---|
| Average time | Θ( n2 ) | poor |
| Worst time | O( n2 ) | poor |
| Worst space | O( 1 ) | good |
Properties
- Stable
- No
- In place
- Yes
- Adaptive
- No
- Comparison-based
- Yes