Advertisement
SepandMeenu

quicksort with a single scan

Mar 16th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- quicksort
  2. quicksort :: Ord a => [a] -> [a]
  3. quicksort [] = []
  4. quicksort (x1:xs) = let
  5.   -- split xs: ( [x >= x1] , [x < x1] ) for x <- xs
  6.   --- splitter: auxiliary function
  7.   auxSplitter x0 geq_lt = let (geq, lt) = geq_lt
  8.     in if x0 >= x1 then (x0:geq, lt)
  9.        else (geq, x0:lt)
  10.            
  11.   --- split tail of list via foldr and auxSplitter
  12.   (geq_x1, lt_x1) = foldr auxSplitter ([], []) xs
  13.                    
  14.   -- do quicksort
  15.   in quicksort lt_x1 ++ [x1] ++ quicksort geq_x1
  16.  
  17.  
  18. -- test
  19. testQuicksort = (quicksort [1, -2, 3, 4, 1, 2] == [-2, 1, 1, 2, 3, 4]) && (quicksort [3] == [3])
  20. -- test1 = quicksort [] == []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement