Simple/dumb QuickSort in RubyLanguage, choosing the first element as pivot.
def qsort(list)
return [] if list.size == 0
x, *xs = *list
less, more = xs.partition{|y| y < x},
qsort(less) + [x] + qsort(more)
end
Slightly less readable but hey, we save a line :P
def qs(l)
return [] if (x,*xs=l).empty?
less, more = xs.partition{|y| y < x},
qs(less) + [x] + qs(more)
end
Slightly more readable, and hey, we save two lines.
def quicksort a
(pivot = a.pop) ? quicksort(a.select{|i| i <= pivot},) + [pivot] + quicksort(a.select{|i| i > pivot},) : []
end
But that, of course, is much too inefficient because it doesn't use partition.