Last active
May 6, 2019 01:51
-
-
Save bil-bas/6107183 to your computer and use it in GitHub Desktop.
Ruby quicksort
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
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 | |
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
hm, the following JS port does not work. it returns a strangely scrambled list with one member less.