noaf3q
Last Updated: February 25, 2016
·
1.314K
· robsonmwoc

Comb Sort

My First contribution to Wikipedia, Hell Yeah!

Today, I was studying sorting algorithms, and I found this strange comb sort algorithm. This algorithm seems to not be so popular as bubble sort or quick sort, but I liked it.

Its page at wikipedia didn't have a ruby example, so I wrote one, I hope that could be helpful.

http://pt.wikipedia.org/wiki/Comb_sort#Ruby

def comb_sort(list)
  shrink_factor = 1.247330950103979
  gap = list.size
  swapped = true

  until (gap == 1) && !swapped
    gap = gap / shrink_factor

    gap = 1 if gap < 1

    i = 0
    swapped = false

    until (i + gap) >= list.size
      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