Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.

The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary by one element to the right.

This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n2), where n is the number of items.’

Consider the following depicted array as an example.

For the first position in the sorted list, the whole list is scanned sequentially. The first position where 14 is stored presently, we search the whole list and find that 10 is the lowest value.

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

After two iterations, two least values are positioned at the beginning in a sorted manner.

The same process is applied to the rest of the items in the array.

Following is a pictorial depiction of the entire sorting process −

`// C program for implementation of selection sort#include <stdio.h>void swap(int *xp, int *yp){int temp = *xp;*xp = *yp;*yp = temp;}void selectionSort(int arr[], int n){int i, j, min_idx;// One by one move boundary of unsorted subarrayfor (i = 0; i < n-1; i++){// Find the minimum element in unsorted arraymin_idx = i;for (j = i+1; j < n; j++)if (arr[j] < arr[min_idx])min_idx = j;// Swap the found minimum element with the first elementswap(&arr[min_idx], &arr[i]);}}/* Function to print an array */void printArray(int arr[], int size){int i;for (i=0; i < size; i++)printf("%d ", arr[i]);printf("\n");}// Driver program to test above functionsint main(){int arr[] = {64, 25, 12, 22, 11};int n = sizeof(arr)/sizeof(arr[0]);selectionSort(arr, n);printf("Sorted array: \n");printArray(arr, n);return 0;}`

In Python

`# Python program for implementation of Selection# Sortimport sysA = [64, 25, 12, 22, 11]# Traverse through all array elementsfor i in range(len(A)):# Find the minimum element in remaining # unsorted arraymin_idx = ifor j in range(i+1, len(A)):if A[min_idx] > A[j]:min_idx = j# Swap the found minimum element with # the first element A[i], A[min_idx] = A[min_idx], A[i]# Driver code to test aboveprint ("Sorted array")for i in range(len(A)):print("%d" %A[i]), `

In Java

`// Java program for implementation of Selection Sortclass SelectionSort{void sort(int arr[]){int n = arr.length;// One by one move boundary of unsorted subarrayfor (int i = 0; i < n-1; i++){// Find the minimum element in unsorted arrayint min_idx = i;for (int j = i+1; j < n; j++)if (arr[j] < arr[min_idx])min_idx = j;// Swap the found minimum element with the first// elementint temp = arr[min_idx];arr[min_idx] = arr[i];arr[i] = temp;}}// Prints the arrayvoid printArray(int arr[]){int n = arr.length;for (int i=0; i<n; ++i)System.out.print(arr[i]+" ");System.out.println();}// Driver code to test abovepublic static void main(String args[]){SelectionSort ob = new SelectionSort();int arr[] = {64,25,12,22,11};ob.sort(arr);System.out.println("Sorted array");ob.printArray(arr);}}`