Created
March 20, 2013 06:24
-
-
Save ackintosh/5202713 to your computer and use it in GitHub Desktop.
Quicksort in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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