public
Created

shell sort

  • Download Gist
sort.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
def get_increase(len)
h = 1
while h < len / 3
h = h * 3 + 1
end
h
end
 
def shell_sort(list)
inc = get_increase(list.length)
while inc >= 1
(inc...list.length).each do |i|
j = i
while j >= inc and list[j] < list[j - inc]
 
temp = list[j]
list[j] = list[j - inc]
list[j - inc] = temp
 
j = j - inc
end
end
inc = inc / 3
end
list
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.