Skip to content

Instantly share code, notes, and snippets.

@bil-bas
Last active May 6, 2019 01:51
Show Gist options
  • Save bil-bas/6107183 to your computer and use it in GitHub Desktop.
Save bil-bas/6107183 to your computer and use it in GitHub Desktop.
Ruby quicksort
module Enumerable
def quicksort()
if size <= 1
self
else
pivot = shift
lower, greater = partition {|x| x < pivot }.map(&:quicksort)
lower.push(pivot).concat(greater)
end
end
end
@bil-bas
Copy link
Author

bil-bas commented Jul 29, 2013

You need >= in the upper check, otherwise if the pivot is duplicated, then the other values that are the same will be dropped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment