Do you think these aspects can be incorporated without adding extra baggage for beginners (the primary target for this visualizations)?
Showing beginners quicksort with mutation pulls back the curtain on functional programming and reveals the wizard. Functional programming, so long as we have Von Neumann machines, is a layer of abstraction and who am I to argue with Abelson and the Sussmans that beginning programmers should be introduced to abstraction layers as quickly as possible?
- if the left arrow points to an element greater than the pivot, swap the elements under the arrows and move the right arrow one cell left
- else if the right arrow points to an element less than the pivot, swap the elements under the arrows and move the left arrow one cell right
- else if one of the arrows points at the pivot, move the other arrow towards it
until the two arrows point at the same cell, which now contains the pivot. You could show the full selection animation the first time and then just move all elements to their final places at once every subsequent time. I would also replace the random pivot selection with selecting the middle-valued element among the first, middle and last element of the array.
I find Mergesort to be much more intuitive. And better yet, it has a much better worst-case performance.
Sure both are time O{n log n}. Sure both are space O{n}. But merge sort is ~2n + c space and quicksort is n + c space. In the days when computers had kilobytes of memory and rooms full of tape drives, the memory efficiency of quicksort made it much quicker.
Which brings up the difference between mathematical worst case performance and engineering worst case performance. Statistically, the use of a random pivot (which was part of Hoare's paper) gives quicksort similar worst case performance to merge sort. In production with 'big data', hitting slow secondary memory such as persistent storage will dominate throughput.
Nope. Quicksort is much worse: time O(n^2). See [1].
[1] https://en.wikipedia.org/wiki/Quicksort#Worst-case_analysis
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?
Besides designing the illustrations, I've spent a lot of time crafting the framework so expect more of these in the future. Ongoing effort, but also tried to make the codebase approachable for others to learn from and be able to contribute: https://github.com/skidding/illustrated-algorithms
How would you make algorithms fun? Looking forward to ideas and feedback. Thanks!
While I'd like to keep the mission of this project to "illustrating the basic mechanics of algorithms as elegantly as possible", I realize this can be a) annoying for people who understand the specifics in depth, and b) not enough (or confusing) for people just picking this up. Which is why I'm thinking of creating an info page for each algorithm to:
- Outline the limitations of the featured version
- List a number of possible improvements (e.g. pivot strategies)
- Link to external resources for complete examples & docs
Open discussion. What do you think?Best thing imo is the idea to go forward and backward through code and see the results live. I'd love to have this to study or debug code, and to really see and understand the magic of some operations in a visualization like this.
The thing is, algorithms like Quicksort are quite involved. In order to understand I'd need at least 2 intermediate steps between the animation and the code:
- The core concepts, like "recursion, in place sorting, complexity/performance average/worst case, trouble when list already sorted" etc.
- The programming ideas, like "we pick a pivot, we use a random element from the list but you could use any", "our recursion stops when there is only 1 item in the sub-list" etc.
If these things were linked to a) the animation and b) the code and I could experience it all at once, that would be amazing (but a lot of work for you of course :-))
Also: - I kept sorting the fruits by size somehow, and due to the language barrier a "P"ineapple would be an "A"nanas for me - If I commit to learning something I like to go to a place that has comprehensive information, even if various places to one thing well. - Preventing zoom and the scrolling code is not ideal imo. - Code could be simpler and clearer. - Maybe the task to explain Quicksort like this is just too huge
> Best thing imo is the idea to go forward and backward through code and see the results live
Yeah, this is the main idea and driver for me. The reason why I chose algorithms is because I could get reasonably small bits of code that accomplish a job on their own, and build something up top of that. Code is already open source and reusable, but turning this into a general purpose tool requires much more dedication. But you're right, introducing algorithms along with this concept also opens Pandora's box in terms of confusion & opinions.
I intended to compare objects of different heights at first, but ended up reusing the emoji tiles from Binary Search to reduce implementation time. I'll try to get more creative with the next (e.g. a maze for BFS).
I have the following comments, hope you find them useful: * I find counter intuitive that the animation goes up, i think it should go down, but that probably just me. * Would recommend using something different than fruit which is probably one of the last things that comes to mind when thinking of ordered sets, also the picture keeps getting in the way of me sorting lexicographically in my mind. * A per step mode would also come in handy.
- Added Disclaimer to clarify the project mission and limitations
- Created Footnotes section for insights collected from the community
Both can be found here: https://github.com/skidding/illustrated-algorithms/blob/mast...Thanks!
Make an array called `equal` before making the `less` and `greater` arrays.
const equal = arr.filter(i => i === pivot);
Then, instead of adding the pivot, add the equal array. return [
...quicksort(less),
...equal,
...quicksort(greater)
]For anyone else who enjoyed this, you might also enjoy this blog post full of animated algorithms by Mike Bostock: https://bost.ocks.org/mike/algorithms/
I found myself wanting to set my own initial order of the list to see what some specific edge cases (already sorted, one element off from being sorted, in reverse order, etc) look like.