Sunday, 5 June 2016

Haskell: implement quick sort using list comprehensions


Sort.hs
qsort [] = []
qsort (pivot:xs) = qsort [x | x<-xs, x<pivot] ++ [pivot] ++ qsort [x | x<-xs, x>=pivot]

Prelude> :load Sort.hs
[1 of 1] Compiling Main             ( Sort.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> qsort []
[]
*Main> qsort [1, 3, 5, 7, 2, 4, 6, 8]
[1,2,3,4,5,6,7,8]


Previous                                                 Next                                                 Home

No comments:

Post a Comment