Wednesday, 31 December 2008

Visualizing Bubble Sort


So I finally finished my rubbish sort visualization. It was more about learning a bit of Clojure interacting with Swing.

The code certainly isn't the prettiest, but it is very concise compared how it'd look in Java. The canvas does the drawing, using proxy to provide implementations of the paintComponent and listen to tick events from the timer.

The bubble-sort function returns a list which represents each intermediate stage of the bubble sort algorithm (well, it's not quite perfect as it includes all the swaps, not one at a time).

An atom is used to represent the mutable state (in this case, a counter which moves between 0 and the number of items in the list).

The full code is available in my Git repository here.