Visualizations and Sorting

I love visualizations. One of my all time favorites are the programs that show how different sorting algorithms work. I was reminded of this by a recent post by Stacey Armstrong (CS News – What do sorts sounds like? ) with a link to a sorting sample that used sound as well as images. You can access the “What different sorting algorithms sound like” imagine at this link. These visualizations are really quite helpful for understanding how these sorts work. In fact writing them is a great exercise for students. But I get asked the question “why bother teaching sorting algorithms when most programmers use library or framework sorts. It’s a fair question.

If I want to sort an array named MyArray in a dot net language I will call the sort method of the Array object. That’s it. One call to a method with a parameter. It’s quick and easy for me as a programmer and the results are a very fast sort. In fact in every test I have done the framework sort method has been faster than any sorting algorithm I can write myself even when I use the best reference books I can find. Truth be told companies hire really brilliant mathematicians and computer scientists to create their library methods. Few can hope to do as well, let alone, better than these people. They bring outstanding brains and unparalleled training to the task. So why have students write sort algorithms?

Several reasons. One is just to study algorithms in general. Looking at different methods of sorting lets you discuss efficiency (think Big-O) using a problem (sorting) that students already understand. Though they probably don’t understand it as well as they think they do.  It also helps to understand complexity of things that appear easy on the surface. Just try asking them how they sort things and watch them struggle to document the process. Along the way we get to explain related concepts such as searching (insertion sort), swapping of objects, loops and nested loops, and for several sorts – recursion! Quick sort is a much better example of recursion than Fibonacci sequence or Towers of Hanoi in my opinion. Students can more easily relate to sorting and that is one of the things that makes for a good exercise.

So it is not so much about the particular problem (sorting) as it is learning the tools and related (or sub) concepts. And if you use visualizations it can even be fun. Are you using visualizations for sorts or other algorithms? What do they look like? Are they working for you in your classes? Please share what works for you.

By the way, I do highly recommend Stacey Armstrong’s blog if you teach computer science. He’s been posting a lot of good stuff.