I noticed some problem that everyone else did not notice for some reason; you have haskell and python code. (please tell me if this has been fixed in automatic optimizations, I don't know anything about optimizations). for this I will demonstrate in haskell. in your code you define smaller and larger lists, for example:
where lesser = filter (<p) xs greater = filter (>=p) xs
this is bad because you are comparing with p each element in xs twice, once to get a smaller list and again to get a larger list. this (theoretically, I did not check the time) makes your variety twice as many comparisons; this is a catastrophe. instead, you should create a function that splits the list into two lists using a predicate, so that
split f xs
equivalently
(filter f xs, filter (not.f) xs)
using this function, you will only need to compare each element in the list once to find out which side of the tuple to put it on.
ok let's do this:
where split :: (a -> Bool) -> [a] -> ([a], [a]) split _ [] = ([],[]) split f (x:xs) |fx = let (a,b) = split f xs in (x:a,b) |otherwise = let (a,b) = split f xs in (a,x:b)
now allows you to replace the smaller / larger generator with
let (lesser, greater) = split (p>) xs in (insert function here)
full code:
quicksort :: Ord a => [a] -> [a] quicksort [] = [] quicksort (p:xs) = let (lesser, greater) = splitf (p>) xs in (quicksort lesser) ++ [p] ++ (quicksort greater) where splitf :: (a -> Bool) -> [a] -> ([a], [a]) splitf _ [] = ([],[]) splitf f (x:xs) |fx = let (a,b) = splitf f xs in (x:a,b) |otherwise = let (a,b) = splitf f xs in (a,x:b)
for some reason I cannot change the role of getter / lesser in where clauses, so I had to do this in let clauses. also, if it's not a recursive tail, let me know and fix it for me (I don't know yet how the tailing dump works)
you should now do the same for python code. I do not know python, so I can not do this for you.
EDIT: in fact, such a function already exists in a Data.List, called a section. Please note that this proves the need for such a function, because otherwise it will not be defined. this compresses the code:
quicksort :: Ord a => [a] -> [a] quicksort [] = [] quicksort (p:xs) = let (lesser, greater) = partition (p>) xs in (quicksort lesser) ++ [p] ++ (quicksort greater)