Selection Sort | kirupa.com

by kirupa | 3 January 2015

A slow but very easy-to-comprehend sort algorithm is selection sort. It's approach is very simple. Let's say you have a boring list of values that you want sorted


This is a companion discussion topic for the original entry at http://www.kirupa.com/sorts/selectionsort.htm

Your description is very good, however you drag too much implementation detail into the basic description. notably when you swap two items to move the smallest into the sorted portion.

You assumption is that the collection to be sorted can only be in an array. But the are many other ordered collection types.

So, in your initial description, you should only mention finding the smallest and moving it to a new collection (which you mention in a passing way as “cray, cray”. This is in fact the best way to handle it if the collection were a linked list. In a addition to be more generic, it will make the whole explanation simpler and easier to understand.

You should then discuss swapping as an optimization for the special case of the collection being an array.

James - that is really good feedback. I’ll revise the tutorial to have the explanation focus on the “separate” collection approach. You are right that having the sorted items in the front is an implementation detail and doesn’t need to be called out so early!

:smile: