The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.

1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.

In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.

Following example explains the above steps:

arr[] = 64 25 12 22 11

// Find the minimum element in arr[0…4]

// and place it at beginning 11 25 12 22 64

// Find the minimum element in arr[1…4]

// and place it at beginning of arr[1…4] 11 12 25 22 64

// Find the minimum element in arr[2…4]

// and place it at beginning of arr[2…4] 11 12 22 25 64

// Find the minimum element in arr[3…4]

// and place it at beginning of arr[3…4] 11 12 22 25 64

C and C++

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20for%20implementation%20of%20selection%20sort%0A%23include%20%3Cstdio.h%3E%0A%20%0Avoid%20swap(int%20*xp%2C%20int%20*yp)%0A%7B%0A%20%20%20%20int%20temp%20%3D%20*xp%3B%0A%20%20%20%20*xp%20%3D%20*yp%3B%0A%20%20%20%20*yp%20%3D%20temp%3B%0A%7D%0A%20%0Avoid%20selectionSort(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20i%2C%20j%2C%20min_idx%3B%0A%20%0A%20%20%20%20%2F%2F%20One%20by%20one%20move%20boundary%20of%20unsorted%20subarray%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20n-1%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Find%20the%20minimum%20element%20in%20unsorted%20array%0A%20%20%20%20%20%20%20%20min_idx%20%3D%20i%3B%0A%20%20%20%20%20%20%20%20for%20(j%20%3D%20i%2B1%3B%20j%20%3C%20n%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bj%5D%20%3C%20arr%5Bmin_idx%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20min_idx%20%3D%20j%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Swap%20the%20found%20minimum%20element%20with%20the%20first%20element%0A%20%20%20%20%20%20%20%20swap(%26arr%5Bmin_idx%5D%2C%20%26arr%5Bi%5D)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Function%20to%20print%20an%20array%20*%2F%0Avoid%20printArray(int%20arr%5B%5D%2C%20int%20size)%0A%7B%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%3D0%3B%20i%20%3C%20size%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20arr%5Bi%5D)%3B%0A%20%20%20%20printf(%22%5Cn%22)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%7B64%2C%2025%2C%2012%2C%2022%2C%2011%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20selectionSort(arr%2C%20n)%3B%0A%20%20%20%20printf(%22Sorted%20array%3A%20%5Cn%22)%3B%0A%20%20%20%20printArray(arr%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”c and c++” highlight=”” provider=”manual”/] [ad type=”banner”]

Python

[pastacode lang=”python” manual=”%23%20Python%20program%20for%20implementation%20of%20Selection%0A%23%20Sort%0Aimport%20sys%0AA%20%3D%20%5B64%2C%2025%2C%2012%2C%2022%2C%2011%5D%0A%20%0A%23%20Traverse%20through%20all%20array%20elements%0Afor%20i%20in%20range(len(A))%3A%0A%20%20%20%20%20%0A%20%20%20%20%23%20Find%20the%20minimum%20element%20in%20remaining%20%0A%20%20%20%20%23%20unsorted%20array%0A%20%20%20%20min_idx%20%3D%20i%0A%20%20%20%20for%20j%20in%20range(i%2B1%2C%20len(A))%3A%0A%20%20%20%20%20%20%20%20if%20A%5Bmin_idx%5D%20%3E%20A%5Bj%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20min_idx%20%3D%20j%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%23%20Swap%20the%20found%20minimum%20element%20with%20%0A%20%20%20%20%23%20the%20first%20element%20%20%20%20%20%20%20%20%0A%20%20%20%20A%5Bi%5D%2C%20A%5Bmin_idx%5D%20%3D%20A%5Bmin_idx%5D%2C%20A%5Bi%5D%0A%20%0A%23%20Driver%20code%20to%20test%20above%0Aprint%20(%22Sorted%20array%22)%0Afor%20i%20in%20range(len(A))%3A%0A%20%20%20%20print(%22%25d%22%20%25A%5Bi%5D)%2C%20″ message=”python” highlight=”” provider=”manual”/]

Java

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20for%20implementation%20of%20Selection%20Sort%0Aclass%20SelectionSort%0A%7B%0A%20%20%20%20void%20sort(int%20arr%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20One%20by%20one%20move%20boundary%20of%20unsorted%20subarray%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n-1%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Find%20the%20minimum%20element%20in%20unsorted%20array%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20min_idx%20%3D%20i%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%20i%2B1%3B%20j%20%3C%20n%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bj%5D%20%3C%20arr%5Bmin_idx%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20min_idx%20%3D%20j%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Swap%20the%20found%20minimum%20element%20with%20the%20first%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20element%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20temp%20%3D%20arr%5Bmin_idx%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bmin_idx%5D%20%3D%20arr%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bi%5D%20%3D%20temp%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Prints%20the%20array%0A%20%20%20%20void%20printArray(int%20arr%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(arr%5Bi%5D%2B%22%20%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println()%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20code%20to%20test%20above%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20SelectionSort%20ob%20%3D%20new%20SelectionSort()%3B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20%7B64%2C25%2C12%2C22%2C11%7D%3B%0A%20%20%20%20%20%20%20%20ob.sort(arr)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Sorted%20array%22)%3B%0A%20%20%20%20%20%20%20%20ob.printArray(arr)%3B%0A%20%20%20%20%7D%0A%7D” message=”java” highlight=”” provider=”manual”/]

 

Output:

Sorted array: 11 12 22 25 64

Time Complexity: O(n2) as there are two nested loops.

Auxiliary Space: O(1)

The good thing about selection sort is it never makes more than O(n) swaps and can be useful when memory write is a costly operation.

[ad type=”banner”]