If you're in computer science, you've probably seen an animation of sorting algorithms, maybe heard a rendition, or seen a visual representation. I have, somewhat by accident, discovered a different way to visualise a sorting algorithm: plot points for memory accesses, with address on the X axis and time (counted by accesses) on the Y axis, and different colours for reads and writes. It produces some rather pretty pictures. Note that these are not to scale relative to each other - the Y axis has been compressed to fit the entire sort into a fixed height.
Ye olde bubblesort. Some of the patterns are an optical illusion due to aliasing, but the green spikes are a feature of the algorithm.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzWYQ2IYdOKqFiYAbHIBrZS0Wgn5LBo6AqRhrI5GdCxqPN_c4nrzEzl0p-PXLeEoN6ipi0gYpob0-XGx1S4qgmyVKOgMlSHYkHSJaNVdEFWzmLtTqzeKbNyx8kWS0rdzk_JP2qI1niQR0/s320/bubble_sort.png)
Insertion sort - the version optimized for mostly sorted content. Although the data is random, you can see that in many cases it reduces the search distance.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgwftrBT-hC4Jlkr5b7g81fY43zO8tDvsZv-cHuCy1LGYydmOhI-QEjSXpKTYhGc6DyqJ0lekHmvve74NY94XZ-uETRI1t2wAQQ7vKM_TC2qYj5eRTCA-QuacliZn-jgz2unnNnXXAfAZc/s320/insertion_sort.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzBnWbD5x38gwIcDuui33N-HAoh6CYqSsUhTCTdBh6l8xvivP6h6x-UlM0HxAyH7kAuwcn4IOG4LFSJNlqoGU0GgbqEEPKL-jrBhHjtBqKjM1M3XAiUAIpPwV1zB99zoU9n_w8jHo6x3A/s320/shell_sort.png)
Selection sort:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJUvY1MtnFH-gakktBfxzKYQAm0RJYC-eQjajJKDQE2QGVghMqW_NMtl6XAQH8pKgGQoD0C0olC4-4PcMBSiNxkGVfy5gi6zuWk9C1QpJbKpT6k_YHppJnhzi5U9LZhaLmltOvODdxuIw/s320/selection_sort.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUs06EsEbanFNoxXw8ozbGin4buOA4buguQ4TuZ6WCZNRSFEl8dPxdf-D8nqmA8ovaGpJMGbW0jpyylB60EUUtS4velyT7ybxSdMQAw_mYVOo4HI66SRSgyPP348_mBfglHJkHOD0DGa0/s320/heap_sort.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcSD3R7H3mZxVRXfJYxdauqMwjO2b44I0heB-HcNQmvmtiaSLoIOd05XaQfbFeTnwUrmyANrzNy_tDh-0VJGFHHRaJqsej3PAcP2etKoleFTkbmfAldexisgYCyA6bZq6q2RKxAxJB7zc/s320/quick_sort.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHipU5dT4yzRb7XpeqEF7iwjekIECa-ZtRd5N1BL-S5I3F1rMnrQESTZup5EQCPjs-9ZEh52zef8Te4B9CVbC7uBVCQdEsEtVs83g4dGqQhyjw9XqFhBNUYnD5-jEg7PnIc8mD0k_tKA8/s320/merge_sort.png)
http://blog.brucemerry.org.za/2010/09/visualising-sorting-algorithms.html
No comments:
Post a Comment