## Introduction

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.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.test; public class SelectionSort { public static void main(String[] args) { SelectionSort ss = new SelectionSort(); int[] a = new int[] { 6, 10, 13, 5, 8, 3, 2, 11 }; ss.printArray(a); ss.selectionSort(a); ss.printArray(a); } public void selectionSort(int[] a){ for(int i = 0 ; i < a.length -1 ; i++){ int min = i; for (int j = i+1 ; j< a.length ; j++){ if(a[j]<a[min]) min=j; } swap(a,i,min); printArray(a); } } public void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } public void printArray(int[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } } |

## Conclusion

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