请输入您要查询的百科知识:

 

词条 Comb sort
释义

  1. Algorithm

      Pseudocode  

  2. See also

  3. References

{{Short description|Interval based sorting algorithm}}{{More citations needed | date = March 2011}}{{Infobox algorithm
|class=Sorting algorithm
|image=
|data=Array
|time=[1]
|average-time=, where {{math|p}} is the number of increments[1]
|best-time=
|space=
|optimal=
}}

Comb sort is a relatively simple sorting algorithm originally designed by Włodzimierz Dobosiewicz in 1980.[1][2] Later it was rediscovered by Stephen Lacey and Richard Box in 1991.[3] Comb sort improves on bubble sort.

Algorithm

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

In bubble sort, when any two elements are compared, they always have a gap (distance from each other) of 1. The basic idea of comb sort is that the gap can be much more 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: [ n/k, n/k2, n/k3, ..., 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. k = 1.3 has been suggested as an ideal shrink factor by the authors of the original article after empirical testing on over 200,000 random lists. 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 1 gap size.

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.2.

Pseudocode

   '''function''' combsort('''array''' input)      gap := input.size // Initialize gap size     shrink := 1.3 // Set the gap shrink factor     sorted := false      '''loop while''' sorted = false
// Update the gap value for a next comb
         gap := floor(gap / shrink)         '''if''' gap ≤ 1             gap := 1             sorted := true // If there are no swaps this pass, we are done         '''end if''' 
// A single "comb" over the input list
         i := 0         '''loop while''' i + gap < input.size // See Shell sort for a similar idea             '''if''' input[i] > input[i+gap]

swap(input[i], input[i+gap])

// If this assignment never happens within the loop,

                 // then there have been no swaps and the list is sorted.              '''end if'''               i := i + 1          '''end loop'''

      '''end loop'''  '''end function'''

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

1. ^{{Cite journal | doi = 10.1016/S0020-0190(00)00223-4| title = Analyzing variants of Shellsort| journal = Inf. Process. Lett.| volume = 79| issue = 5| pages = 223–227| date = 15 September 2001| last1 = Brejová | first1 = B. }}
2. ^{{Cite journal |title=An efficient variation of bubble sort |author=Wlodzimierz Dobosiewicz |journal=Information Processing Letters |volume=11 |year=1980 |pages=5–6 |doi=10.1016/0020-0190(80)90022-8}}
3. ^"A Fast Easy Sort", Byte Magazine, April 1991
{{sorting}}{{DEFAULTSORT:Comb Sort}}

3 : Sorting algorithms|Comparison sorts|Articles with example pseudocode

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/17 15:04:37