Last active
July 24, 2019 07:53
-
-
Save kelvin8773/5d0d5b43735d0524c15de00f6c14ff08 to your computer and use it in GitHub Desktop.
quick sorting method in Ruby
This file contains 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
def quicksort(array) | |
def partition(array) | |
pivot,left, right =array[0], [], [] | |
array[1..-1].each do |x| | |
x > pivot ? right << x : left << x | |
end | |
[left, pivot , right] | |
end | |
return array if array.size <= 1 | |
left, pivot , right = partition(array) | |
res = quicksort(left) + [pivot] + quicksort(right) | |
return res | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment