{{Short description|Interval based sorting algorithm}} {{Infobox algorithm |class=Sorting algorithm |image=Visualisation of comb sort |caption=Comb sort with shrink factor ''k''=1.24733 |data=Array |time=<math>O(n^2)</math><ref name=BB>{{Cite journal | doi = 10.1016/S0020-0190(00)00223-4 | title = Analyzing variants of Shellsort | last1 = Brejová | first1 = Bronislava | journal = Information Processing Letters | volume = 79 | issue = 5 | pages = 223–227 | date = 15 September 2001 | url = https://archive.org/download/wikipedia-scholarly-sources-corpus/10.1016%252Fs0016-2361%252802%252900113-8.zip/10.1016%252FS0020-0190%252800%252900223-4.pdf }}</ref> |average-time=<math>\Omega(n^2/2^p)</math>, where {{math|''p''}} is the number of increments<ref name=BB/> |best-time=<math>\Theta(n \log n)</math> |space=<math>O(1)</math> |optimal= }} '''Comb sort''' is a relatively simple sorting algorithm originally designed by Włodzimierz Dobosiewicz and Artur Borowy in 1980,<ref name=BB/><ref>{{Cite journal |title=An efficient variation of bubble sort |first=Wlodzimierz |last=Dobosiewicz |journal=Information Processing Letters |volume=11 |issue=1 |date=29 August 1980 |pages=5–6 |doi=10.1016/0020-0190(80)90022-8 }}</ref> later rediscovered (and given the name "Combsort") by Stephen Lacey and Richard Box in 1991.<ref>{{cite news |url=http://cs.clackamas.cc.or.us/molatore/cs260Spr03/combsort.htm |title=A Fast, Easy Sort: A novel enhancement makes a bubble sort into one of the fastest sorting routines |first1=Stephen |last1=Lacey |first2=Richard |last2=Box |journal=Byte Magazine |department=Hands On |date=April 1991 |volume=16 |issue=4 |pages=315–318, 320 |archive-url=https://web.archive.org/web/20210927224138/http://cs.clackamas.cc.or.us/molatore/cs260Spr03/combsort.htm |archive-date=2021-09-27 |url-status=dead }} [https://archive.org/details/eu_BYTE-1991-04_OCR Entire magazine] available at archive.org.</ref> Comb sort improves on bubble sort in the same way that Shellsort improves on insertion sort, in that they both allow elements that start far away from their intended position to move more than one space per swap.

NIST's Dictionary of Algorithms and Data Structures defines comb sort and Shellsort as types of "diminishing increment sort"<ref>{{cite web |first1=Paul E. |last1=Black |first2=Art S. |last2=Kagel |title=diminishing increment sort |website=Dictionary of Algorithms and Data Structures |date=7 January 2005 |publisher=National Institute of Standards and Technology |url=https://xlinux.nist.gov/dads/HTML/diminishingIncSort.html |accessdate=March 9, 2021 }}</ref>, but mentions that Don Knuth uses that name as a synonym for Shellsort, while other authors use the name "comb sort" for the entire class.

==Algorithm== The basic idea is to eliminate ''turtles'', small values near the end of the list, since in a bubble sort these slow the sorting down tremendously. ''Rabbits'', large values near the beginning of the list, do not pose a problem for bubble sort.

In bubble sort, when any two elements are compared, they always have a ''gap'' (distance from each other) of 1.<ref>{{cite web |first1=Paul E. |last1=Black |first2=Art S. |last2=Kagel |title=comb sort |website=Dictionary of Algorithms and Data Structures |date=21 April 2022 |publisher=National Institute of Standards and Technology |url=https://xlinux.nist.gov/dads/HTML/combSort.html |accessdate=March 11, 2026}}</ref> The basic idea of comb sort is that the gap can be much larger than 1. The inner loop of bubble sort, which does the actual ''swap'', is modified such that the gap between swapped elements goes down (for each iteration of outer loop) in steps of a "shrink factor" ''k'': {{math|[{{sfrac|''n''|''k''}}, {{sfrac|''n''|''k''<sup>2</sup>}}, {{sfrac|''n''|''k''<sup>3</sup>}}, ..., 1]}}.

The gap starts out as the length of the list ''n'' being sorted divided by the shrink factor ''k'' (generally 1.3; see below) and one pass of the aforementioned modified bubble sort is applied with that gap. Then the gap is divided by the shrink factor again, the list is sorted with this new gap, and the process repeats until the gap is 1. At this point, comb sort continues using a gap of 1 until the list is fully sorted. The final stage of the sort is thus equivalent to a bubble sort, but by this time most turtles have been dealt with, so a bubble sort will be efficient.

The shrink factor has a great effect on the efficiency of comb sort. Dobosiewicz suggested ''k'' = 4/3 = 1.333…, while Lacey and Box suggest 1.3 as an ideal shrink factor after empirical testing on over 200,000 random lists of length approximately 1000. A value too small slows the algorithm down by making unnecessarily many comparisons, whereas a value too large fails to effectively deal with turtles, making it require many passes with a gap of 1.

The pattern of repeated sorting passes with decreasing gaps is similar to Shellsort, but in Shellsort the array is sorted completely each pass before going on to the next-smallest gap. Comb sort's passes do not completely sort the elements. This is the reason that Shellsort gap sequences have a larger optimal shrink factor of about 2.25.

One additional refinement suggested by Lacey and Box is the "rule of 11": always use a gap size of 11, rounding up gap sizes of 9 or 10 (reached by dividing gaps of 12, 13 or 14 by 1.3) to 11. This eliminates turtles surviving until the final gap-1 pass.

===Pseudocode=== '''function''' combsort('''array''' input) '''is''' gap := input.size <span style="color:green">// Initialize gap size</span> shrink := 1.3 <span style="color:green">// Set the gap shrink factor</span> sorted := false '''loop while''' sorted = false <span style="color:green">// Update the gap value for a next comb</span> gap := floor(gap / shrink) '''if''' gap ≤ 1 '''then''' gap := 1 sorted := true <span style="color:green">// If there are no swaps this pass, we are done</span> '''else if''' gap = 9 '''or''' gap = 10 '''then''' gap := 11 <span style="color:green">// The "rule of 11"</span> '''end if''' <span style="color:green">// A single "comb" over the input list</span> i := 0 '''loop while''' i + gap < input.size<span style="color:green"> // See Shell sort for a similar idea</span> '''if''' input[i] > input[i+gap] '''then''' swap(input[i], input[i+gap]) sorted := false <span style="color:green">// If this assignment never happens within the loop, // then there have been no swaps and the list is sorted.</span> '''end if''' i := i + 1 '''end loop''' '''end loop''' '''end function''' <!-- Please do not modify the pseudocode to make corrections unless you have verified, by translating the pseudocode to an actual programming language, that the corrections actually *are* corrections. Especially don't try to optimize it at the expense of clarity or understandability. -->

== See also == {{Wikibooks|Algorithm Implementation|Sorting/Comb sort|Comb sort}} * Bubble sort, a generally slower algorithm, is the basis of comb sort. * Cocktail sort, or bidirectional bubble sort, is a variation of bubble sort that also addresses the problem of turtles, albeit less effectively.

== References == {{reflist}}

== External links == * {{cite tech report |title=An analysis of Comb Sort |first=Felipe Vaiano |last=Calderan<!--See Portuguese name; surnames are Vaiano Calderan, but Portuguese convention is to alphabetize by final surname--> |publisher=Federal University of São Paulo (UNIFESP) |date=May 2022 |url=https://fvcalderan.github.io/myworks/articles/comb_sort.pdf |access-date=11 March 2026 }}

{{sorting}}

{{DEFAULTSORT:Comb Sort}} Category:Comparison sorts Category:Articles with example pseudocode Category:Articles with example Python (programming language) code