PDA

View Full Version : Sorting a list of strings


weedweaver
11-13-2003, 01:19 PM
how do i go about sorting a list of strings in haskell e.g. on input:

["Noradh","Marges","letsee","telegrams","Sharan's","sees"]

weedweaver
11-13-2003, 02:05 PM
its ok, i got it... just a simple quicksort algorithm:

sortList :: [String] -> [String]
sortList [] = []
sortList (x:xs) = sortList [ y | y <- xs, y<=x] ++ [x] ++ sortList [ y | y <- xs, y > x]

jemfinch
12-31-2003, 07:13 AM
That quicksort is inefficient, turning what should be an O(n log n) (normal case) algorithm into O(n**2).

Use merge sort instead. It's just as easy to write and guaranteed to be O(n log n).

Jeremy