Floorplanner Tech Blog
Our geek adventures
Selection sort in JavaScript
In my quest to learn more about algorithms, I came across another sorting algorithm. This one is called Selection sort. The collection is sorted from beginning to end. While looping over the elements, it first finds the smallest element between the current position and the end of the collection. Then the smallest element and the current element exchange places. This way all elements are sorted when the loop has finished. In JavaScript it looks like this:
var collection = [5, 2, 4, 6, 1, 3];
var n = collection.length;
for(var j = 0; j