Selection sort is a sorting algorithm which works by selecting the smallest element (in Ascending order case) and swapping it with the starting element of the array.

The selection sort is a combination of searching and sorting. During each pass, the unsorted element with the smallest or largest value is moved to its proper position in the array. The number of times the sort passes through the array is one less than the number of items in the array. In the selection sort, the inner loop finds the next smallest or largest value and the outer loop places that value into its proper location.

It can be considered that the array is divided into two parts during iterations, the starting part of the array containing the sorted data and the ending part contains the unsorted data. The sorted part (the sorted part) grows gradually and unsorted part (the ending part) shrinks as the algorithm works. It might sound confusing but the concepts will be cleared after looking at the animated example and step by step example below.

Let's consider the below example, which explains each step one by one.

Consider an array ( 2 1 5 3 4 ). Let’s apply selection sort on this array.

First Iteration

( 2 1 5 3 4 )        Starting at zero index, ‘2’ is the current value, We will iterate further to find the smallest value.

( 2 1 5 3 4 )        While moving on, we find a smaller value. We will store the index at which we found the smaller element and continue to iterate further in the array.

( 2 1 5 3 4 )

( 2 1 5 3 4 )

( 2 1 5 3 4 )         When we reached the end, ‘1’ was the smallest value. We substitute 1 with the element at index from which we started, i.e zero index which contains ‘2’.

1 2 5 3 4 )

Second Iteration

( 1 2 5 3 4 )          Now start from index 1 where ‘2’ is stored as the elements before index 1 are already sorted. 2 is the smallest number at the moment.

( 1 2 5 3 4 )          value is greater than 2

( 1 2 5 3 4 )          value is greater than 2

( 1 2 5 3 4 )          value is greater than 2. Means 2 is the smallest and is already at the start.

Third Iteration

( 1 2 5 3 4 )          Now starting at index 2 where ‘5’ is stored. The elements before index 2 are already sorted. Elements after index 2 are unsorted. 5 is the smallest number at the moment.

( 1 2 5 3 4 )          3 is smaller than 5 thus the smallest number is updated.

( 1 2 5 3 4 )          value is greater than 3

( 1 2 3 5 4 )         When we reached the end, ‘3’ was the smallest value. We substitute 3 with the element at index from which we started, i.e second index which contains ‘5’.

Fourth iteration

( 1 2 3 5 4 )         Now starting at index 3 where ‘5’ is stored. The elements before index 3 are already sorted. Elements after index 3 are unsorted. 5 is the smallest number at the moment.

( 1 2 3 5 4 )          4 is smaller than 5 thus the smallest number is updated. Also reached the end thus substitute 4 with the element at index from whichwe started, i.e third index which contains ‘5’.

( 1 2 3 4 5)           The array is now sorted.

Selection Sort Java Program

package SelectionSort;

public class SelectionSortProgram 
{
	public static void sort( int arr[] ){

		int i, j, pos, temp;

		int arrLength = arr.length;

		for (i = 0; i < arrLength-1; i++)
		{
			pos = i;
			for (j = i+1; j < arrLength; j++)
			{
				if (arr[j] < arr[pos])
				{
					pos = j;
				}
			}
			temp = arr[i];
			arr[i] = arr[pos];
			arr[pos]= temp;            
		}        
	}
	public static void main(String[] args) 
	{
		int[] arr1 = {-6,10,34,2,56,7,67,88,42,0};
		
		System.out.println("array before sorting \n");
		
		for(int i:arr1){
			System.out.print(i);
			System.out.print(", ");
		}
		
		System.out.println("\n\n array after sorting \n");
		
		sort(arr1);
		
		for (int i=0; i< arr1.length; i++)
		{
			System.out.print(arr1[i]+ ", ");
		}
		
	}
}

The output of the above code

array before sorting 

-6, 10, 34, 2, 56, 7, 67, 88, 42, 0, 

 array after sorting 

-6, 0, 2, 7, 10, 34, 42, 56, 67, 88, 

That's it we are done, if you have any questions related to the above article, feel free to post it.

You may also like

Palindrome program in java (String & number example)

Pyramid Triangle pattern programs in Java with explanation

C program to print pyramid pattern, explained with code example