r/dailyprogrammer • u/Elite6809 1 1 • Sep 18 '14
[9/19/2014] Challenge #180 [Hard] Sorting Visualisation
(Hard): Sorting Visualisation
This challenge is up a bit early as I'm busy tomorrow so I'll probably forget. Anyway, after reading the comments on this week's Weekly Discussion, I wrote this week's Hard challenge based on two commonly requested things:
- Graphical visualization
- Usage of algorithms
and I decided to combine the two. This will also be a relatively open-ended challenge, as they seem to be quite popular among the developers - i.e. you - here. For this challenge, you will input a set of real numbers, and visualise the sorting of that set into ascending order, with an algorithm(s) of your choice, with any mode of visualisation you can imagine.
Input Description
You will be given a set of numbers that are between 0 and 1 (inclusive). The method of input is up to you.
Output Description
Visualise the sorting of the data, in a step-by-step manner, in any way you like. It can be console-based, graphical based, web-based, 3D based or even physically with an Arduino or the like, if you're feeling particularly adventurous!
Further Reading
To get to grips with some different sorting algorithms, let's look at four here.
Bubble Sort
Bubble sort is the simplest of the four. You simply step through the list, looking at pairs of elements that are next to each other. If the pair is not in order, you swap them and look at the next pair, like so:
4 1 2 3 5
<->
1 4 2 3 5
<->
1 2 4 3 5
<->
1 2 3 4 5
x
1 2 3 4 5If the list is not sorted after doing this, you go through the list again until it is. Done! This is simple but slow. Onto the next one...
Selection Sort
Selection sort is, to me, the most intuitive of the four, and is probably similar to what you do when you sort a pack of playing cards. Simply start with your list L and an empty list S. While L is not empty, move the lowest value from L to the end of S, like so:
[3 5 6 1 8 7 2 4] []
[3 5 6 8 7 2 4] [1]
[3 5 6 8 7 4] [1 2]
[5 6 8 7 4] [1 2 3]
[5 6 8 7] [1 2 3 4]
[6 8 7] [1 2 3 4 5]
[8 7] [1 2 3 4 5 6]
[8] [1 2 3 4 5 6 7]
[] [1 2 3 4 5 6 7 8]And now S is our sorted list. Simple again, however this too is slow on larger lists.
Merge Sort
Fast and surprisingly simple, once you get your head round it. First, split the list into lists with only 1 item:
[3] [5] [6] [1] [8] [7] [2] [4]Then, take pairs of lists and merge them. How to merge them, you say? It's fairly simple - to merge lists A and B into new list C, do the following. While A and B are not empty, look at the first item in both lists. Append the lowest of the two to the end of list C. If either A or B is empty and the other isn't, just put the remaining items from the non-empty list at the end of C. OK.
Now, we have the following lists after merging 3 times:
[3 5] [1 6] [7 8] [2 4]
[1 3 5 6] [2 4 7 8]
[1 2 3 4 5 6 7 8]The final list there is your list in order. Done!
Quicksort
This one is perhaps the most difficult of the four, but it's still not too hard. The first step is to take the list - let's call it L - and partition it into two halves, with a 'pivot' value in the middle. A good way to choose the pivot is to pick 3 random values from L and choose the median. Anyway, after we've split the list in half - into two lists, A and B - we look at the elements in A, which we will make our lower list, and compare each value against the pivot value. If the element is greater than the pivot value, put it at into list B (our higher list), in no particular position. Now, do the same for list B; look at each element and see if it is lower than the pivot. If it is, put it into list A at no particular position. Our list L now looks like:
A pivot Bnow sort A and B the same way we sorted L. If A or B contain either no elements or one element, it is already sorted, and if there are only 2 values, you can just swap them.
Stuck?
Here are a few videos to kick-start your imagination!
15
u/le_donger Sep 19 '14 edited Sep 19 '14
C++
Used SFML for drawing. I visualized every value with a grey color. For now I did selection sort, will probably update later with other algorithms and/or other visualization method.
http://i.imgur.com/D8eTMu2.gif
Code (updated): https://gist.github.com/foolish314159/e341657534e27cffc582
EDIT: Added bubble sort.
Vector size 20, redrawn after every swap: http://i.imgur.com/Dl2P9bU.gif
Vector size 500, redrawn after every full iteration of the vector: http://i.imgur.com/SGJvXNW.gif
EDIT2: Added threading
Now using threads for the sorting and displaying both (bubble and selection sort) at the same time. Code got a bit messy as I have been rushing it and added some global vars. Ain't nobody got time to do that properly. Also first time using threading in C++, not sure if what I am doing is any good, but atleast I get the result I want.
http://i.imgur.com/0YyY3nn.gif (vector size 200 for both)
6
u/skeeto -9 8 Sep 19 '14 edited Sep 20 '14
C99. Animates your system's libc's
qsort(), which, despite the name, isn't necessarily quicksort. That is to say, the output changes depending what operating system you're using. It works by drawing snapshots of the partially-sorted array while in the comparator function. Also, please excuse the global variables. They're necessary becauseqsort()'s interface doesn't allow for re-entrant comparators.If you look carefully in glibc's GIFs, you'll notice the final step isn't fully animated, only the red "pointers" are. In the first case, this is because mergesort is not an in-place sort and I don't have access to glibc's temporary internal array. In the quicksort case, it does a partial sort, then an insertion sort on a private array at the end, hence the full scan.
In Cygwin's quicksort you can see it looking around for a good pivot at the start.
On OpenBSD you can see the insertion sort kicking in near where recursion bottoms out.
It outputs to stdout a bunch of concatenated Netpbm PPM images. The PPM header defines the exact size of the image, so it's trivial to pull these apart. ImageMagick's
convertcommand understands this and can convert the program's output directly into an animated GIF (above).And finally, the code:
5
u/G33kDude 1 1 Sep 18 '14 edited Sep 20 '14
BubbleSort! (In AutoHotkey)
https://gist.github.com/G33kDude/aa97dfeb5b1729f269ce
http://i.imgur.com/TPp6spA.gif
Edit: Now slower, but prettier
Edit: Now with quicksort! I sleep for 1 second at the start so you can get a good view of the board
https://gist.github.com/G33kDude/f4ba4825c8b069bc9b3f
http://i.imgur.com/SDVFDJ5.gif
(Crashes sometimes, haven't figured out why)
Edit: Figured out why. I had a few typos in the partition function, causing the pivot to never be put back into the center. This led me to modify the quicksort function to include the pivot in the second call, so that it could be sorted in properly. This leads to a fun behavior of infinite recursion if there is more than one instance of the pivot value.
After fixing that problem I found the additional problem that when the whole array is the same value, I reach the maximum recursion depth before finishing the sort. Instead of changing partition to sort all the pivots into the center, I took the cheaters way out and just put half of the extra pivots on one side, and the other half on the other side.
2
u/TieSoul 0 1 Sep 27 '14 edited Sep 27 '14
Ruby + Tkinter
The quicksort visualization is, ironically, very slow because tk sucks.
2
u/dohaqatar7 1 1 Oct 05 '14 edited Oct 09 '14
TI-Basic Visualized Insertion Sort
Only works for lists that are length 16 or less due to the home screen being only 16 characters wide. Like wise, all values are scaled into the range [1,8] because the home screen in only 8 characters tall.
The values are represented by columns of
0, expect for the columns that is being examined(1).This could be quickly modified to function on the graph screen, allowing for larger ranges in the lists, and longer lists, but the time it takes to draw becomes unreasonably long.
3
u/Regimardyl Sep 19 '14
For the curious, here is a website dedicated to visualized algorithms: http://www.algomation.com/
1
u/[deleted] Sep 22 '14
JavaScript & HTML5 Canvas
Codepen demo
It it recursive, please don't look at the try/catch part it is an ugly solution.
0
u/[deleted] Sep 18 '14
[deleted] — view deleted comment