DEV Community

Discussion on: Quicksort In Javascript

Collapse
 
ogzhanolguncu profile image
Oğuzhan Olguncu • Edited

I used this example to teach the idea of quicksorting and recursion. This is not a real-world example. By the way, you can easily get rid of shift() using first item as an index arr[0] and start iterating by staring from index 1. And I don't think ... gives you any side effects.

But you might be right about push(). If you truly improve the performance of your sort, you should really focus on choosing better pivots I picked the first for simplicity. Maybe try using Median of three(First,Middle,Last) or maybe median of three random elements to avoid O(n2).

So anyway, I'm open for improvements to ease out the learning curve of this algorithm.

Collapse
 
faiwer profile image
Stepan Zubashev

... doesn't give you any side-effect, you're right. It's FP pure. But it is copying the whole array every time :) That's where the problem is.

If you truly improve the performance of your sort, you should really focus on choosing better pivots I picked the first for simplicity

I think you may even set pivot equal 0 each iteration and even then it would be faster than version with ... :)

... ruins this algorithm. Most of algorithms have to be impure to be useful.
So you can dramatically improve it by getting rid of excess copies. I think it'd be better even in educational point of view