Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- quicksort
- quicksort :: Ord a => [a] -> [a]
- quicksort [] = []
- quicksort (x1:xs) = let
- -- split xs: ( [x >= x1] , [x < x1] ) for x <- xs
- --- splitter: auxiliary function
- auxSplitter x0 geq_lt = let (geq, lt) = geq_lt
- in if x0 >= x1 then (x0:geq, lt)
- else (geq, x0:lt)
- --- split tail of list via foldr and auxSplitter
- (geq_x1, lt_x1) = foldr auxSplitter ([], []) xs
- -- do quicksort
- in quicksort lt_x1 ++ [x1] ++ quicksort geq_x1
- -- test
- testQuicksort = (quicksort [1, -2, 3, 4, 1, 2] == [-2, 1, 1, 2, 3, 4]) && (quicksort [3] == [3])
- -- test1 = quicksort [] == []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement