Have you tried writing a non-recursive partition implementation? I suspect the performance difference is just a partition implementation. You do recursion for each element of your implementation.
Update
Here is a quick implementation. It is still not very fast or even efficient, but it is much better than the original recursive one.
>>> def partition(data): ... pivot = data[0] ... less, equal, greater = [], [], [] ... for elm in data: ... if elm < pivot: ... less.append(elm) ... elif elm > pivot: ... greater.append(elm) ... else: ... equal.append(elm) ... return less, equal, greater ... >>> def qsort2(data): ... if data: ... less, equal, greater = partition(data) ... return qsort2(less) + equal + qsort2(greater) ... return data ...
I also think that in the "traditional" version there are more temporary lists.
D.Shawley
source share