Selection sort is a simple sorting algorithm which starts by splitting the input array into two parts, a sorted part and an unsorted part, the unsorted part is then iterated through and the smallest element is swapped with the element at the start of the unsorted part.
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
void selectionSort(vector<int>& arr) {
for (int i = 0; i < arr.size(); i++) {
int m = i;
for (int j = m; j < arr.size(); j++) {
if (arr[j] < arr[m]) {
minIndex = j;
}
}
swap(arr[i], arr[m]);
}
}
Selection sort can sometimes be useful in a few systems where reads are super quick but writes are very expensive and where there isn't a large amount of data.
Looking at the two for loops, we can see that the first loop always iterates from 0 to arr.size(), the inner loop on the other hand iterates from i to arr.size(). Using this knowledge we can construct a formula for the runtime
where arr.size()
In asymptotic terms we can see that the quantity of swap operations in this algorithm is modelled by
Firstly, we can read through the code and see the form of the minIndex function which was already proven in this example. We can thus assume that m is always the index of the smallest unsorted element. Using this knowledge we can see that the function swaps the element at index i with the element at the index of the smallest unsorted element m.
Running through a few examples we can see that arr[0..i-1] always contains the
Loop Invariant: Before iteration i of the loop, arr[0..i-1] contains the i smallest elements of arr in ascending order.
Base Case: i=0
i=0, the invariant states that arr[0..-1] contains the 0 smallest elements of arr in ascending orderarr[0..-1] contains no elements the invariant holds in the base case.Inductive Step: i = k, 0 <= k < arr.size()
k, ie. arr[0..k-1] contains the k smallest elements of arr in ascending order.arr[k..arr.size()-1]arr[k]k arr[0..k] contains the k+1 smallest elements of arr in ascending order and the loop invariant holds.Termination: i=arr.size()
arr[0..arr.size()-1] contains the arr.size() smallest elements of arr in ascending orderarr.size() is finite, and i increments each iteration towards arr.size() the program must terminate.