How selection sort works – A detailed explanation


Probably the most crude sorting technique, and also the most in-efficient one. But it’s important, because until we know the worst we can’t understand the importance of the good and the better. So let us try to find out, how selection sort works.

Purpose of the article

We will try to understand here, how selection sort works and how is the nature of input affecting the running time of this algorithm. Why this is a crude way of sorting and its advantages and dis-advantages.

Idea behind Selection Sort

As the name suggests, there is definitely something to select. Lets assume we are trying to sort a list of N numbers in ascending order. To start with, we theoretically divide the array into two parts, sorted and unsorted. Initially the sorted part has no elements, the unsorted part is the complete array as below, where the orange shade shows unsorted and the green part is showed as sorted.

We maintain a counter i, at the beginning of the unsorted part of the array, and another counter j which traverse through the complete unsorted part. j tries to search for the smallest element in the array by traversing through the unsorted part and when j reaches the end , we already know the index of the smallest element.

Now we swap the smallest element with the element at location i. For e.g.: To start with in the above array we have i set to the first element, i.e. index 0, j is set to i+1, a variable ‘min’ stores the index of smallest element and as j travels right, the index min may change if we find a smaller number in the unsorted section.
So, when j reaches the end of the array the value of min would be 6 (because the smallest element lies at index 6 in the above array). Now we swap the element at i with the element at index min. And the array will look like below:


We increase the value of i by 1 and repeat the same procedure, till i reaches the end of the array. Now, if we analyze this, the loop for j runs through out the unsorted array every time to find the smallest element. This loop makes the algorithm very much in-efficient, because there is no way to find if a given number is the smallest number in a list, until we compare it with all the numbers.

Below is the position of the elements after each iteration of the inner loop or may be after each increment in the value of the counter i.

Iteration 2 : img3

Iteration 3 : img4

Iteration 4 : img5

Iteration 5 : img6

Iteration 6 : img7

Iteration 7 : img8

Iteration 8 : img9

The reason I said in the beginning that it is crude algorithm, because the whole procedure of searching the smallest number in the unsorted list and putting it at the end of the sorted part is trivial and consumes lot of time.

Code and Implementation


We learnt that what ever is the nature of the input, in each iteration we need to find the smallest element from the unsorted part which is a linear time operation, as we all know that finding smallest element in an array by traversing through the array is a task which has O(n) running time complexity.

And this task has to be repeated N-1 times. Hence, the complexity would be quadratic time. So we can say the running time is 1+2+3+.. N, which equals to N(N+1)/2 , that by definition is quadratic time complexity.

Hope this help, happy learning. Feel free to write comments and ask questions. Stay connected and stay Subscribed