joesingo.co.uk

Sorting Algorithms

4th August 2018

This page demonstrates various sorting algorithms with short animations.

Numbers are represented by lines with differing heights. The red lines show the current number (or pair of numbers) that is being considered (or compared).

Press 'Run' beneath each code sample to run the code and see the animation.

Note: The draw_list() calls are only there to generate the animation -- they are not part of the algorithm!

Bubble sort

Selection sort

Insertion sort

Merge sort

Note: Here the sub-lists are first merged into a new list (sorted) which is copied into the original list afterwards. This new list is not shown in the animation, which is why the sub-lists appear to magically combine into one after the comparisons are shown.

Quick sort

Bubble sort in Brainfuck

As a bonus, here is bubble sort implemented in Brainfuck.

>+[++++++++++++++++++++++++++++++++>,--------------------------------]<[<]>[-]>>[[<[[>]>[>]>+>>+<<<<[<]<[<]>-]+[>]>[>]>[-<<[<]<[<]>+[>]>[>]>]<<[<]<[<]>->[[>]>[>]>+>>>+<<<<<[<]<[<]>>-]+[>]>[>]>[-<<[<]<[<]>>+[>]>[>]>]<<[<]<[<]>>-[>]>[>]>>>[->-[-<<+<+>>>]<<[->>+<<][-]+<[>-<[-]]>[<<<[<]<[<]>>[-<<+>>]<[->+<]<[->+<]>[>]>[>]>>>[-]<-]>]>[-]<<<<<[<]<[<]>[-<+>]>>]<[->+<]<<[[->+<]<]>>>]<[->+<]>[.>][-]++++++++++.

Try it out with your favourite Brainfuck interpreter (I have tested it with fabianishere/brainfuck, copy.sh/brainfuck/ and fatiherikli.github.io/brainfuck-visualizer).

These Brainfuck interpreters read ASCII characters as input, so the program will sort characters by their ASCII representation. The list of characters must be terminated by a space.

I have a commented version in a Gist which is probably easier to read!

Matrix transformations