Selection Sort
Gustavo Reis Bauer
Posted on February 4, 2024
Selection Sort
Selection sort, is a really famous and easy sorting algorithm, that is quite slow, but it is so trivially simple it is the most common one to teach first.
Ok, but what does a sorting algorithm do?
A sorting algorithm puts a set of values in an order according to a comparator, so for example, let's say we have an array of numbers and we want to put it into ascending order for convenience, or because we want to perform binary search on it.
What does it mean to put an array of numbers in ascending order? It means that every single element follows the maxim that every single element must be bigger than the previous, but smaller than the next, mathematically speaking
For each element Xn of an array A, [ Xn <= X(n+1) and Xn >= X(n-1) ]
So for example the array: [1, 10, 2, 4, 129, 0, 4] would be: [0, 1, 2, 4, 4, 10, 129]
The algorithm
The main idea of this algorithm is, go through all the elements and find the index of the smallest element, and then if the index is not the same as we currently are, swap the elements
Implementation
function selectionSort(array) {
for (let i = 0; i < array.length; ++i) {
let lowest = i;
for (let j = i + 1; j < array.length; ++j) {
if (array[j] < array[lowest]) lowest = j;
}
if (lowest !== i) {
const aux = array[i];
array[i] = array[lowest];
array[lowest] = aux;
}
}
}
Ok, cool, and what is the complexity of this algorithm? Considering that, for each element we are trying to find the smallest which require us to go through each element inside of the loop, therefore, the complexity is O(n*n), making this algorithm terribly slow for big arrays, since it grows as bases of power two, so 1, 2, 4, 8, 16, 32, ...
There is one really cool implementation detail in this algorithm that is the swapping part, since we cannot just store the value of array[lowest]
inside of array[i]
because if we dit it like this, we would wipe the list with the same element, so we have to keep a pointer to the value of the old element before we wipe it with the value of the smallest one. This operation is often called swapping elements
In most languages there is specific syntax for that, we could do it in javascript like this: [array[i], array[lowest]] = [array[lowest], array[i]];
, but I thought that would make such a simple and beautiful idea obtuse and kinda hard to follow
Posted on February 4, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.