Created
February 27, 2013 07:12
-
-
Save aergonaut/5045878 to your computer and use it in GitHub Desktop.
Basic quick sort implementation, unoptimized
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 qsort(list) | |
return list unless list.length > 1 | |
pivot_index = rand(0...list.length) | |
pivot = list[pivot_index] | |
less = [] | |
greater = [] | |
list.delete_at(pivot_index) | |
list.each do |el| | |
(if el > pivot then greater else less end) << el | |
end | |
(qsort(less) << pivot << qsort(greater)).flatten | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment