-
-
Save thurloat/1752010 to your computer and use it in GitHub Desktop.
Quicksort in Haskell & Python
This file contains hidden or 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
qs = (arr) -> | |
[].concat qs(i for i in arr when i < arr[0]), [arr[0]], qs(i for i in arr when i > arr[0]) if arr.length <= 1 else arr |
This file contains hidden or 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
qsort (p:xs) = qsort [x | x<-xs, x<p] ++ [p] ++ qsort [x | x<-xs, x>=p] |
This file contains hidden or 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
def qs(array): | |
if len(array) == 0 or len(array) == 1: | |
return array | |
p = array[0] | |
lesser = [i for i in array if i < p] | |
greater = [i for i in array if i > p] | |
return qs(lesser) + [p] + qs(greater) | |
# Or even | |
def qs(ar): | |
if len(ar) == 0 or len(ar) == 1: | |
return ar | |
p = ar[0] | |
return qs([i for i in ar if i < p]) + [p] + qs([i for i in ar if i > p]) |
This file contains hidden or 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
quicksort :: Ord a => [a] -> [a] | |
quicksort [] = [] | |
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater) | |
where | |
lesser = filter (< p) xs | |
greater = filter (>= p) xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment