Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Created March 20, 2013 06:24
Show Gist options
  • Save ackintosh/5202713 to your computer and use it in GitHub Desktop.
Save ackintosh/5202713 to your computer and use it in GitHub Desktop.
Quicksort in Ruby
class Array
def quick_sort
return self if length <= 1
base = pop #pivot
smaller, bigger = partition { |v| v <= base }
push base #for keep original
smaller.quick_sort + [base] + bigger.quick_sort
end
end
ar = [5,3,2,7,10,2,12]
p ar.quick_sort
# [2, 2, 3, 5, 7, 10, 12]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment