Skip to content

Instantly share code, notes, and snippets.

@vderyagin
Last active December 11, 2015 07:09
Show Gist options
  • Save vderyagin/4564660 to your computer and use it in GitHub Desktop.
Save vderyagin/4564660 to your computer and use it in GitHub Desktop.
quicksort implementation in haskell
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