Selection Sort is a really easy to implement sorting algorithm, due to being inefficient on large datasets this algorithm is mostly used when memory is limited.
Selection sort work by having a list of numbers, it then goes through the list and finds the minimum number. When we finished looping the list we swap the founder minimum number with the current index.
As an example we take the following list:
We start at index 0, and start finding the minimal element in this sub list, which results in 0. We now swap index 0 with the index of the found minimum and get that we will swap 2 and 0 resulting into:
Now we are at index 1 (value 9), we find the minimum value again which is at index 6 (value 1), so we swap those 2
And we continue doing this until we have a sorted list:
For the implementation check paragraph §3.4.
1.3.2. Advantages and Disadvantages
Advantages
Uses almost no swaps! $\Theta(n)$
No extra space required, $O(n)$
Disadvantages
The performance analysis is really easy for selection sort, it will always perform $n^2$ because we loop the list no matter what.
1.3.4. Implementation
1.3.4.1 Pseudo Code
1.3.4.2 C++ Code
1.3.5. Benchmark
1.3.6. Conclusion
We can clearly see that selection sort is a horrible sorting algorithm, however when swaps are really expensive and the memory is limited we could consider using this algorithm.