Therefore, given a size N of the input array, the selection sort algorithm has the following time and complexity values. Now as per selection sort, we will start from the first element and look for the smallest number in the array, which is 1 and we will find it at the index 2.
Fact, that selection sort requires n - 1 number of swaps at most, makes it very efficient in situations, when write operation is significantly more expensive, than read operation. Note that the selection sort technique never takes more than O n swaps and is beneficial when the memory write operation proves to be costly.
Every step of outer loop requires finding minimum in unsorted part.
Therefore, selection sort makes n steps n is number of elements in array of outer loop, before stop. Overall algorithm complexity is O n2.
Algorithm The idea of algorithm is quite simple. One for loop steps through all the elements in the array and we find the minimum element index using another for loop which is nested inside the outer for loop.
Finding Smallest Element in a subarray In selection sort, in the first step, we look for the smallest element in the array and replace it with the element at the first position.
So, we will now look for the smallest element in the subarray, starting from index 1, to the last index. This is repeated, until the array is completely sorted.
Selection sort works best when the range of the values to be sorted is known.