TheAlgorithms-Ruby/sorting/comb_sort.rb
2021-05-06 10:13:25 +03:00

22 lines
464 B
Ruby

def comb_sort(array)
gap = array.length
swaps = true
while gap > 1 or swaps
gap = [1, (gap / 1.25).to_i].max
swaps = false
0.upto(array.length - gap - 1) do |i|
if array[i] > array[i + gap]
array[i], array[i + gap] = array[i + gap], array[i]
swaps = true
end
end
end
array
end
if $0 == __FILE__
puts 'Enter a list of numbers separated by space'
list = gets.split.map(&:to_i)
p insertion_sort(list)
end