Last active
December 11, 2015 07:09
-
-
Save vderyagin/4564660 to your computer and use it in GitHub Desktop.
quicksort implementation in haskell
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
| import Data.List | |
| assert :: Bool -> () | |
| assert False = error "assertion failed" | |
| assert True = () | |
| qSort :: Ord a => [a] -> [a] | |
| qSort [] = [] | |
| qSort [x] = [x] | |
| qSort (x:xs) = qSort less ++ [x] ++ qSort more | |
| where (less, more) = partition (< x) xs | |
| qSort' :: Ord a => [a] -> [a] | |
| qSort' [] = [] | |
| qSort' [x] = [x] | |
| qSort' all@(x:_) = qSort less ++ qSort more | |
| where (less, more) = partition (< x) all | |
| assert (qSort [5, 4, 3, 2, 1] == [1, 2, 3, 4, 5]) | |
| assert (qSort' [5, 4, 3, 2, 1] == [1, 2, 3, 4, 5]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment