How do you implement a quick sort in Haskell?

Answer:

-- empty list is already sorted
qsort [] = []

-- choose first element as pivot,
-- put all elements less than x on the left,
-- put all elements greater than x on the right,
-- recurse on both sides
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)

First answer by Moobler. Last edit by Moobler. Contributor trust: 354 [recommend contributor recommended]. Question popularity: 1 [recommend question].