Skip to content

Instantly share code, notes, and snippets.

@iamvery
Last active August 29, 2015 14:07
Show Gist options
  • Save iamvery/9ac31a6694d1f0d2de50 to your computer and use it in GitHub Desktop.
Save iamvery/9ac31a6694d1f0d2de50 to your computer and use it in GitHub Desktop.
Quicksort in Ruby
def qsort(list)
return list if list.empty?
x, *xs = list
smaller, larger = xs.partition { |n| n < x }
qsort(smaller) + [x] + qsort(larger)
end
describe 'qsort' do
it 'sorts a list of numbers' do
list = [4,8,3,2,6,2,1]
sorted_list = qsort(list)
expect(sorted_list).to eq([1,2,2,3,4,6,8])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment