I am learning haskell and the function definition I see is:
quickSort (x : xs) = (quickSort less) ++ (x : equal) ++ (quickSort more)
where less = filter (< x) xs
equal = filter (== x) xs
more = filter (> x) xs
Is it possible to write it with only one traversal of the list, instead of 3?
It does not seem to improve anything but:
Although late, here's a version that's supposed to not leak space as much (and seems to run about twice faster than the other 3-way version here):
This addresses the possible problem with using tuples, where
let (a,b) = ...
is actually translated intolet t= ...; a=fst t; b=snd t
which leads to the situation where even aftera
has been consumed and processed, it is still kept around alive, as part of the tuplet
, forb
to be read from it - though of course completely unnecessary. This is known as "Wadler pair space leak" problem. Or maybe GHC (with-O2
) is smarter than that. :)Also this apparently uses difference lists approach (thanks, hammar) which also makes it a bit more efficient (about twice faster than the version using tuples). I think
part
uses accumulator parameters, as it builds them in reversed order.You mean something like this?
Note that this isn't really quicksort, as the real quicksort is in-place.