r/dailyprogrammer • 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 5

If 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 B

now 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!

22 Upvotes

17 comments sorted by

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)

3

u/MotherOfTheShizznit Sep 20 '14 edited Sep 20 '14

Hello, I'm your resident <algorithm> nazi! How do you do? :)

  1. std::shuffle will shuffle your container for you.
  2. std::is_sorted will tell you if your container is sorted.

Edit: nice job, btw.

2

u/le_donger Sep 20 '14

Hey, thanks for the feedback. I'm not using C++ very frequently, so I had no idea this stuff was there, I also seem to forget all the nice features of STL and C++11 :)

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 because qsort()'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 convert command understands this and can convert the program's output directly into an animated GIF (above).

$ ./anim_sort 128 | convert -delay 10 ppm:- sort.gif

And finally, the code:

#include <stdio.h>
#include <stdlib.h>

int array_count;
int *array_base;

void draw_array(int a, int b)
{
    int width = 10, height = 100;
    printf("P6\n%d %d\n255\n", width * array_count, height);
    for (int y = 0; y < height; y++) {
        int edge = y < height / 10 || y > height - height / 10;
        for (int x = 0; x < width * array_count; x++) {
            int xx = x / width;
            int selected = xx == a || xx == b;
            int r = 0, g = 0, b = 0;
            if (edge && selected)
                r = 255;
            else
                r = g = b = (array_base[xx] * 255) / array_count;
            printf("%c%c%c", r, g, b);
        }
    }
}

int intcmp(const void *a, const void *b)
{
    const int *ia = (int *) a, *ib = (int *) b;
    draw_array(ia - array_base, ib - array_base);
    return *ia - *ib;
}

void shuffle()
{
    for (int i = array_count - 1; i > 0; i--) {
        int swap = rand() % (i + 1);
        int tmp = array_base[i];
        array_base[i] = array_base[swap];
        array_base[swap] = tmp;
    }
}

int main(int argc, char **argv)
{
    array_count = argc > 1 ? atoi(argv[1]) : 64;
    int array[array_count];
    for (int i = 0; i < array_count; i++)
        array[i] = i;
    array_base = array;
    shuffle();
    draw_array(-1, -1);
    qsort(array, array_count, sizeof(array[0]), intcmp);
    draw_array(-1, -1);
    return 0;
}

3

u/mrhthepie Sep 28 '14

This is an awesome solution, really clever the way you've injected the visualisation into the system algorithm. I can't get over how cool the idea is.

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

require 'tk'
def initbarchart(canvas, list, width = 3, offset = 0)
  list.each_index do |i|
    TkcRectangle.new(canvas, width*i+2+offset, (list.max)*width, width*i+2+(width-1)+offset, (list.max-list[i])*width + 1)
  end
end
def clear(canvas)
  canvas.find('all').each do |x|
    canvas.delete x
  end
end
def visual_bubblesort(canvas, list, width = 3, asc = true)
  clear(canvas)
  canvas.height = (list.max+1) * width
  canvas.width = (list.length+1) * width
  initbarchart(canvas, list, width)
  done = false
  until done
    done = true
    list.each_index do |i|
      unless i == 0
        if asc
          cond = list[i-1] > list[i]
        else
          cond = list[i-1] < list[i]
        end
        if cond
          done = false
          list[i], list[i-1] = list[i-1], list[i]
          canvas.find('all')[i].coords[1] = 5
          c = canvas.find('all')[i].coords
          canvas.find('all')[i].coords = [c[0], (list.max-list[i])*width + 1, c[2], c[3]]
          c = canvas.find('all')[i-1].coords
          canvas.find('all')[i-1].coords = [c[0], (list.max-list[i-1])*width + 1, c[2], c[3]]
          canvas.update
        end
      end
    end
  end
  list
end

def visual_selectsort(canvas, list, width=3, asc = true)
  clear(canvas)
  canvas.height = (list.max+1) * width
  canvas.width = (list.length+1) * width
  initbarchart(canvas, list, width)
  newlist = []
  while list != []
    el = list.min
    list.delete list.min
    if not asc
      newlist = [el] + newlist
    else
      newlist << el
    end
    clear(canvas)
    initbarchart(canvas, list+newlist, width)
    canvas.update
  end
  newlist
end

def merge(list1, list2, asc)
  newlist = []
  if list1.nil?
    list1 = []
  end
  if list2.nil?
    list2 = []
  end
  while list1 != [] and list2 != []
    one = list1[0]
    two = list2[0]
    if asc
      cond = one < two
    else
      cond = one > two
    end
    if cond
      newlist << one
      list1.delete_at(0)
    else
      newlist << two
      list2.delete_at(0)
    end
  end
  if list1 != []
    newlist = newlist + list1.to_a
  end
  if list2 != []
    newlist = newlist + list2
  end
  newlist
end

def visual_mergesort(canvas, list, width=3, asc = true)
  lists = list.map {|x| [x]}
  clear(canvas)
  canvas.height = (list.max+1) * width
  canvas.width = (list.length+1) * width
  initbarchart(canvas, lists.flatten, width)
  canvas.update
  while lists.length != 1
    newlist = []
    templists = lists.map {|x| x.clone}
    (0...lists.length).step(2) do |i|
      newlist << merge(templists[i], templists[i+1], asc)
      lists = lists.drop(1); lists = lists.drop(1)
      clear(canvas)
      initbarchart(canvas, newlist.flatten + lists.flatten, width)
      canvas.update
    end
    lists = newlist
  end
  lists[0]
end

def visual_quicksort(canvas, list, width=3, asc=true)
  clear(canvas)
  canvas.height = (list.max+1) * width
  canvas.width = (list.length+1) * width
  initbarchart(canvas, list, width)
  @max = list.max
  iter_quicksort(canvas, list, width, asc)
end

def iter_quicksort(canvas, list, width=3, asc=true, offset=0)
  if list.length == 0 or list.length == 1
    return list
  end
  if list.length == 2
    if asc
      cond = list[0] < list[1]
    else
      cond = list[0] > list[1]
    end
    return cond ? list : list.reverse
  end
  pivot = [list.sample, list.sample, list.sample].sort[1]
  a = list[0...(list.length/2).floor] - [pivot]
  b = list[(list.length/2).floor..-1] - [pivot]
  unless a.is_a? Array
    a = [a]
  end
  unless b.is_a? Array
    b = [b]
  end
  tempa = a.clone
  tempa.each do |x|
    if asc
      cond = x > pivot
    else
      cond = x < pivot
    end
    if cond
      b << x
      a -= [x]
      a.each_index do |i|
        c = canvas.find('all')[i+offset].coords
        canvas.find('all')[i+offset].coords = [c[0], (@max - a[i])*width, c[2], c[3]]
      end
      c = canvas.find('all')[a.length+offset].coords
      canvas.find('all')[a.length+offset].coords = [c[0], (@max - pivot)*width, c[2], c[3]]
      b.each_index do |i|
        c = canvas.find('all')[i+offset+a.length+1].coords
        canvas.find('all')[i+offset+a.length+1].coords = [c[0], (@max - b[i])*width, c[2], c[3]]
      end
      canvas.update
    end
  end
  tempb = b.clone
  tempb.each do |x|
    if asc
      cond = x < pivot
    else
      cond = x > pivot
    end
    if cond
      a << x
      b -= [x]
      a.each_index do |i|
        c = canvas.find('all')[i+offset].coords
        canvas.find('all')[i+offset].coords = [c[0], (@max - a[i])*width, c[2], c[3]]
      end
      c = canvas.find('all')[a.length+offset].coords
      canvas.find('all')[a.length+offset].coords = [c[0], (@max - pivot)*width, c[2], c[3]]
      b.each_index do |i|
        c = canvas.find('all')[i+offset+a.length+1].coords
        canvas.find('all')[i+offset+a.length+1].coords = [c[0], (@max - b[i])*width, c[2], c[3]]
      end
      canvas.update
    end
  end
  list = iter_quicksort(canvas, a, width, asc, offset, delay) + [pivot] + iter_quicksort(canvas, b, width, asc, offset + a.length+1, delay)
  list.each_index do |i|
    c = canvas.find('all')[i+offset].coords
    canvas.find('all')[i+offset].coords = [c[0], (@max - list[i])*width, c[2], c[3]]
  end
  list
end    
puts "Set lists or self-defined? (answer 'set' or 'self')"
until %w(set self).include?(p = gets.chomp.downcase)
  puts "Set lists or self-defined? (answer 'set' or 'self')"
end

set = p == 'set'

root = TkRoot.new
@canvas = TkCanvas.new(root)
@canvas.grid sticky: 'nwes', :column => 0, :row => 0
TkGrid.columnconfigure( root, 0, weight: 1 )
TkGrid.rowconfigure( root, 0, weight: 1 )
@canvas.find('all').each do |el|
  @canvas.delete el
end
if set
  root.bind('Key-b', proc{p visual_bubblesort( @canvas, (1..100).to_a.shuffle, 3, true ) == (1..100).to_a})
  root.bind('Key-B', proc{p visual_bubblesort(@canvas, (1..100).to_a.shuffle, 3, false) == (1..100).to_a.reverse})
  root.bind('Key-s', proc{p visual_selectsort(@canvas, (1..400).to_a.shuffle, 1, true ) == (1..400).to_a})
  root.bind('Key-S', proc{p visual_selectsort(@canvas, (1..400).to_a.shuffle, 1, false) == (1..400).to_a.reverse})
  root.bind('Key-m', proc{p visual_mergesort( @canvas, (1..500).to_a.shuffle, 1, true ) == (1..500).to_a})
  root.bind('Key-M', proc{p visual_mergesort( @canvas, (1..500).to_a.shuffle, 1, false) == (1..500).to_a.reverse})
  root.bind('Key-q', proc{p visual_quicksort(@canvas, (1..100).to_a.shuffle, 3, true) == (1..1000).to_a})
  root.bind('Key-Q', proc{p visual_quicksort(@canvas, (1..100).to_a.shuffle, 3, false)== (1..1000).to_a.reverse})
else
  puts "Input a sequence of numbers (the list to sort) in the format 1,2,3,4,5 or 1 2 3 4 5"
  seq = (gets.chomp.split(/([, ])+/)).map {|x| x.to_i}
  seq -= [0]
  p seq
  root.bind('Key-b', proc{p visual_bubblesort( @canvas, seq.clone, 400 / seq.max, true, 3.0 / seq.length) == seq.sort})
  root.bind('Key-B', proc{p visual_bubblesort(@canvas, seq.clone, 400 / seq.max, false, 3.0 / seq.length) == seq.sort.reverse})
  root.bind('Key-s', proc{p visual_selectsort(@canvas, seq.clone, 400 / seq.max, true , 3.0 / seq.length) == seq.sort})
  root.bind('Key-S', proc{p visual_selectsort(@canvas, seq.clone, 400 / seq.max, false, 3.0 / seq.length) == seq.sort.reverse})
  root.bind('Key-m', proc{p visual_mergesort( @canvas, seq.clone, 400 / seq.max, true , 3.0 / seq.length) == seq.sort})
  root.bind('Key-M', proc{p visual_mergesort( @canvas, seq.clone, 400 / seq.max, false, 3.0 / seq.length) == seq.sort.reverse})
  root.bind('Key-q', proc{p visual_quicksort(@canvas, seq.clone, 400 / seq.max, true, 3.0 / seq.length) == seq.sort})
  root.bind('Key-Q', proc{p visual_quicksort(@canvas, seq.clone, 400 / seq.max, false, 3.0 / seq.length)== seq.sort.reverse})
end
puts "Entering Tk main loop. List of controls:"
puts "b - bubblesort, B - desc bubblesort"
puts "s - selectsort, S - desc selectsort"
puts "m - mergesort, M - desc mergesort"
puts "q - quicksort, Q - desc quicksort"
Tk.mainloop

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.

:Prompt L1
:For(I,1,dim(L1)-1
	:L1(I→A
	:I-1→J
	:Lbl A
		:if J <=0
			:goto B
		:If A<=L1(J
			:goto B
		:L1(J→L1(J+1
		:J-1→J
		:goto A
	:Lbl B
	:A→L1(J+1
	
	:ClrHome
	:8/max(L1→Y
	:dim(L1
	:For(X,1,(16<=Ans)16+Ans(16>Ans
		:For(Z,9-int(L1(X)Y),8
			:Output(Z,X,(X=I
		:End
	:End
:End

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.

// By Leon
 
 
// Set up canvas
 
var canvas = document.getElementById('bubble');
var ctx = canvas.getContext('2d');
 
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
 
var bars = [], numberOfBars = canvas.width / 20.5, sortSpeed = 100;
 
// Push bars to array
 
for (var i = 0; i < numberOfBars; i++) {
	bars.push({
		x: i * 20.2, // space between bars
		y: 0,
		w: 15,
		h: Math.floor(Math.random() * (canvas.height - 100) + 20), // generate random height for bars
		color: '#2f2f2f'
	});
}
 
// Draw all the bars by looping through them
 
function draw(){
	ctx.clearRect(0,0,canvas.width,canvas.height)
 
	for (var i = 0, x = bars.length; i < x; i++) {
		var b = bars[i];
 
		ctx.fillStyle = b.color;
		ctx.fillRect(b.x, b.y, b.w, b.h);
 
		ctx.strokeStyle = 'white';
		ctx.strokeRect(b.x, b.y, b.w, b.h);
	}
}
 
// Sort all the bars
 
function sort(i, j) {
	draw();
 
	try {
		if (bars[i].h > bars[0].h){
		  	var b = bars[i].h;
			bars[i].h = bars[0].h;
			bars[0].h = b;
			setTimeout(function(){
				sort(i - 1, j - 1);
			}, 0.001);
		}
 
		if (bars[i].h < bars[j].h) {
			var b = bars[i].h;
			bars[i].h = bars[j].h;
			bars[j].h = b;
			setTimeout(function(){
				sort(i - 1, j - 1);
			}, sortSpeed);
		}
	} catch (err){
		return false;
	}
 
	if (j === bars.length - 1){
		setTimeout(function(){
			sort(bars.length * 0.5, bars.length * 0.5 + 1);
		}, sortSpeed);
	}
 
	setTimeout(function(){
		sort(i + 1, j + 1);
	}, sortSpeed);
}
 
// Start everything up
 
sort(0,1);

0

u/[deleted] Sep 18 '14

[deleted] — view deleted comment

4

u/Elite6809 1 1 Sep 18 '14

Yes. You will be {0.3, 0.17, 0.639, 0.22}.

Fixed.