Skip to content

Instantly share code, notes, and snippets.

@aergonaut
Created February 27, 2013 07:12
Show Gist options
  • Save aergonaut/5045878 to your computer and use it in GitHub Desktop.
Save aergonaut/5045878 to your computer and use it in GitHub Desktop.
Basic quick sort implementation, unoptimized
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