Last active
August 29, 2015 14:13
-
-
Save RuiAAPeres/3ef7af142a8958aaa62a to your computer and use it in GitHub Desktop.
Quicksort translated from https://www.haskell.org/haskellwiki/Introduction#Quicksort_in_Haskell
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
func curry<T,U,V>(f:(T,U)->V) -> T -> U -> V | |
{ | |
return {x in { y in f(x,y)}} | |
} | |
extension Array { | |
var decompose : (T,[T])? { | |
return (count > 0) ? (self[0], Array(self[1..<count])) : nil | |
} | |
} | |
func quicksort(unsorted : [Int]) -> [Int] | |
{ | |
switch (unsorted.decompose) { | |
case .Some(let head, let tail): | |
return quicksort(filter(tail, curry(>)(head))) + [head] + quicksort(filter(tail,curry(<=)(head))) | |
default: | |
return [] | |
} | |
} | |
quicksort([2,1,4,0,1,4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment