by baby · 9y ago · 0 comments
1) on quicksort:
const less = list.filter(i => i < pivot);
const greater = list.filter(i => i > pivot);
Wouldn't this be faster: less = what's < pivot
greater = the rest
2) What is the algorithm people use?Yes, there is a bug in this code. It doesn't handle repeated elements.
If you fix the code by including an equality in one of the two groups, then it works, but it degrades when there are many repeated elements. In the worst case, you can only shrink your groups by one each time, which means the complexity changes to O(n^2) :(
You can fix THIS issue using a 3-way partition. That is the algorithm I've used. With a good pivot choice (random or median of three), this gives you an in-place sort which is guaranteed to be O(nlgn). Nice!
2) What do you mean?