The only way I'll keep learning more algorithms. :)
EDIT: Currently, I'm pursuing a certification so I'm pausing 1algo1week project.
Comb sort
Comb sort is similar to the bubble sort algorithm but with different gaps every cycle. After empirical testing, authors suggested the shrinking factor of 1.3.
Ruby implementation:
def comb_sort(arr)
gap = arr.size
swap = true
while swap or gap > 1
gap = [1, (gap / 1.3).to_i].max
swap = false
0.upto(arr.size - gap - 1) do |i|
if arr[i] > arr[i + gap]
arr[i], arr[i + gap] = arr[i + gap], arr[i]
swap = true
end
end
end
arr
end
print(comb_sort([17, 22, 5, 26, 30, 1, 45]))
#=> [1, 5, 17, 22, 26, 30, 45]