programming-examples/ruby/Algorithms/shell_sort.rb

23 lines
430 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
# Shellsort algorithm,
# see examples in test/shell_sort_test.rb
module Shell
def self.sort(array)
gap = 1
gap = 3 * gap + 1 while gap < array.size / 3
while gap > 0
gap.upto(array.size - 1) do |i|
j = i
while j > 0 && array[j] < array[j - gap]
array[j], array[j - gap] = array[j - gap], array[j]
j -= gap
end
end
gap /= 3
end
array
end
end