Created
December 23, 2019 18:33
-
-
Save thinkphp/f3e9d9f18a27a8e185b3d48a3db7ae2d to your computer and use it in GitHub Desktop.
Combo Sort Algorithm in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def comb_sort( list ) | |
shrinkFactor = 1.3 | |
swapped = true | |
n, gap = list.size, list.size | |
until (gap == 1) && !swapped | |
swapped = false | |
gap = (gap / shrinkFactor) | |
gap = 1 if gap < 1 | |
i = 0 | |
until i + gap >= n | |
if list[ i ] > list[ i + gap ] | |
list[ i ], list[ i + gap ] = list[ i + gap], list[ i ] | |
swapped = true | |
end | |
i = i + 1 | |
end | |
end | |
list | |
end | |
list = [9, 8, 7, 6, 5, 4, 3, 2, 1] | |
print comb_sort( list ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment