You'll need a modern browser that handles SVG (a recent Chrome, Firefox, or IE9 will do) to properly see this post.
On Saturday, Mike Bostock posted a link on Hacker News to an explanation of the Fisher-Yates Shuffle he had written. Alongside the explanation he included a very slick visualization where you could see it in action. Now, at a fundamental level, shuffle algorithms and sorting algorithms are simply the reverse of each other. So if this visualization does well on shuffle algorithms, why not a sorting algorithm as well?
A couple years ago I wrote a sorting algorithm, which I gave the tongue-in-cheek name Marriage Sort (an homage to the problem that inspired it - see the introduction post for more details). I was looking for a project to play with on the weekend, so I thought, why not try the visualization on that? Mike has kindly let me re-use his code, so here's the same visualization of Marriage Sort.
Disclaimer: The visualization I'm building off looks great across platforms, so if there is anything wrong with this one, it's from my changes. Go see the original if you don't believe me.For those that don't remember the algorithm, here's a synopsis. There are two stages:
- Stage 1:
- Take a prefix of size √m, where m is the size of the working set.
- Pick the maximum (in this case, most-right-leaning) of the prefix, and take it as the pivot.
- Walk through the list, pulling out anything greater than the pivot and moving it to the end of the working set.
- Move the pivot to the end of the working set.
- Go back to step 1 and repeat.
- Stage 2: a bog-standard insertion sort.
Looks good to me! What do you think?