Selection sort
Selection sort is another simple sorting algorithm. It is a stable sorting algorithm. In bubble sort, we find that in every pass, if adjacent elements are not in order, they are swapped. Selection sort provides an improvement over bubble sort, with one swapping in every pass. In every pass, it finds out the largest or the smallest element and puts it in the right position. Consider that we have a sequence of integers.
Our sequence has [12,10,16,11,9,7] as elements. We can understand how selection sort works on this given sequence, with the help of the following diagram:
In the first pass of selection sort, the algorithm will look for the smallest element in the entire sequence. After finding the smallest element's location, it will swap it with the first element. In our given sequence, the smallest element in the first pass is 7, which is in the last position in the sequence. After selecting this element, it will be swapped with the first element, 12.
In the second pass...