Last active
November 13, 2015 05:59
-
-
Save mgechev/04d50aa62abb94992e9b to your computer and use it in GitHub Desktop.
Quick sort with ECMAScript array comprehensions
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
(defn qs [lst] | |
(let [[head & tail] lst] | |
(if (>= 1 (count lst)) | |
lst | |
(flatten [(qs (for [x tail :when (<= x head)] x)) head (qs (for [x tail :when (> x head)] x))])))) |
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
-- Haskell implementation | |
quicksort [] = [] | |
quicksort (x:xs) = smallerSorted ++ [x] ++ biggerSorted | |
let smallerSorted = quicksort [a | a <- xs, a <= x] | |
biggerSorted = quicksort [a | a <- xs, a > x] |
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
// JavaScript implementation | |
let qs = (arr) => { | |
if (!arr || !arr.length) return []; | |
let pivot = arr.pop(); | |
return qs([for (x of arr) if (x <= pivot) x]) | |
.concat(pivot) | |
.concat(qs([for (y of arr) if (y > pivot) y])); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment