# Recursive Selection Sort

The Selection Sort algorithm sorts maintains two parts.

1. First part that is already sorted
2. Second part that is yet to be sorted.

The algorithm works by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the end of sorted part.

```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 ```

We have already discussed about Iterative Selection Sort. In this article recursive approach is discussed. The idea of recursive solution is to one by one increment sorted part and recursively call for remaining (yet to be sorted) part.

## C++

 `// Recursive C++ program to sort an array ` `// using selection sort ` `#include ` `using` `namespace` `std; ` ` `  `// Return minimum index ` `int` `minIndex(``int` `a[], ``int` `i, ``int` `j) ` `{ ` `    ``if` `(i == j) ` `        ``return` `i; ` ` `  `    ``// Find minimum of remaining elements ` `    ``int` `k = minIndex(a, i + 1, j); ` ` `  `    ``// Return minimum of current and remaining. ` `    ``return` `(a[i] < a[k])? i : k; ` `} ` ` `  `// Recursive selection sort. n is size of a[] and index ` `// is index of starting element. ` `void` `recurSelectionSort(``int` `a[], ``int` `n, ``int` `index = 0) ` `{ ` `    ``// Return when starting and size are same ` `    ``if` `(index == n) ` `       ``return``; ` ` `  `    ``// calling minimum index function for minimum index ` `    ``int` `k = minIndex(a, index, n-1); ` ` `  `    ``// Swapping when index nd minimum index are not same ` `    ``if` `(k != index) ` `       ``swap(a[k], a[index]); ` ` `  `    ``// Recursively calling selection sort function ` `    ``recurSelectionSort(a, n, index + 1); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = {3, 1, 5, 2, 7, 0}; ` `    ``int` `n = ``sizeof``(arr)/``sizeof``(arr); ` ` `  `    ``// Calling function ` `    ``recurSelectionSort(arr, n); ` ` `  `    ``//printing sorted array ` `    ``for` `(``int` `i = 0; i

## Java

 `// Recursive Java program to sort an array ` `// using selection sort ` ` `  `class` `Test ` `{ ` `    ``// Return minimum index ` `    ``static` `int` `minIndex(``int` `a[], ``int` `i, ``int` `j) ` `    ``{ ` `        ``if` `(i == j) ` `            ``return` `i; ` `      `  `        ``// Find minimum of remaining elements ` `        ``int` `k = minIndex(a, i + ``1``, j); ` `      `  `        ``// Return minimum of current and remaining. ` `        ``return` `(a[i] < a[k])? i : k; ` `    ``} ` `      `  `    ``// Recursive selection sort. n is size of a[] and index ` `    ``// is index of starting element. ` `    ``static` `void` `recurSelectionSort(``int` `a[], ``int` `n, ``int` `index) ` `    ``{ ` `          `  `        ``// Return when starting and size are same ` `        ``if` `(index == n) ` `           ``return``; ` `      `  `        ``// calling minimum index function for minimum index ` `        ``int` `k = minIndex(a, index, n-``1``); ` `      `  `        ``// Swapping when index nd minimum index are not same ` `        ``if` `(k != index){ ` `           ``// swap ` `           ``int` `temp = a[k]; ` `           ``a[k] = a[index]; ` `           ``a[index] = temp; ` `        ``} ` `        ``// Recursively calling selection sort function ` `        ``recurSelectionSort(a, n, index + ``1``); ` `    ``} ` `      `  `     `  `    ``// Driver method ` `    ``public` `static` `void` `main(String args[])  ` `    ``{ ` `        ``int` `arr[] = {``3``, ``1``, ``5``, ``2``, ``7``, ``0``}; ` `      `  `        ``// Calling function ` `        ``recurSelectionSort(arr, arr.length, ``0``); ` `      `  `        ``//printing sorted array ` `        ``for` `(``int` `i = ``0``; i< arr.length; i++) ` `            ``System.out.print(arr[i] + ``" "``); ` `    ``} ` `}  `

## Python3

 `# Recursive Python3 code to sort ` `# an array using selection sort ` ` `  `# Return minimum index ` `def` `minIndex( a , i , j ): ` `    ``if` `i ``=``=` `j: ` `        ``return` `i ` `         `  `    ``# Find minimum of remaining elements ` `    ``k ``=` `minIndex(a, i ``+` `1``, j) ` `     `  `    ``# Return minimum of current  ` `    ``# and remaining. ` `    ``return` `(i ``if` `a[i] < a[k] ``else` `k) ` `     `  `# Recursive selection sort. n is  ` `# size of a[] and index is index of  ` `# starting element. ` `def` `recurSelectionSort(a, n, index ``=` `0``): ` ` `  `    ``# Return when starting and  ` `    ``# size are same ` `    ``if` `index ``=``=` `n: ` `        ``return` `-``1` `         `  `    ``# calling minimum index function  ` `    ``# for minimum index ` `    ``k ``=` `minIndex(a, index, n``-``1``) ` `     `  `    ``# Swapping when index and minimum  ` `    ``# index are not same ` `    ``if` `k !``=` `index: ` `        ``a[k], a[index] ``=` `a[index], a[k] ` `         `  `    ``# Recursively calling selection ` `    ``# sort function ` `    ``recurSelectionSort(a, n, index ``+` `1``) ` `     `  `# Driver code ` `arr ``=` `[``3``, ``1``, ``5``, ``2``, ``7``, ``0``] ` `n ``=` `len``(arr) ` ` `  `# Calling function ` `recurSelectionSort(arr, n) ` ` `  `# printing sorted array ` `for` `i ``in` `arr: ` `    ``print``(i, end ``=` `' '``) ` `     `  `# This code is contributed by "Sharad_Bhardwaj".  `

## C#

 `// Recursive C# program to sort an array ` `// using selection sort ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``// Return minimum index ` `    ``static` `int` `minIndex(``int` `[]a, ``int` `i, ``int` `j) ` `    ``{ ` `        ``if` `(i == j) ` `            ``return` `i; ` `     `  `        ``// Find minimum of remaining elements ` `        ``int` `k = minIndex(a, i + 1, j); ` `     `  `        ``// Return minimum of current and remaining. ` `        ``return` `(a[i] < a[k])? i : k; ` `    ``} ` `     `  `    ``// Recursive selection sort. n is size of  ` `    ``// a[] and index is index of starting element. ` `    ``static` `void` `recurSelectionSort(``int` `[]a, ``int` `n,  ` `                                   ``int` `index) ` `    ``{ ` `         `  `        ``// Return when starting and size are same ` `        ``if` `(index == n) ` `        ``return``; ` `     `  `        ``// calling minimum index function  ` `        ``// for minimum index ` `        ``int` `k = minIndex(a, index, n - 1); ` `     `  `        ``// Swapping when index and minimum index  ` `        ``// are not same ` `        ``if` `(k != index) ` `        ``{ ` `            ``// swap ` `            ``int` `temp = a[k]; ` `            ``a[k] = a[index]; ` `            ``a[index] = temp; ` `        ``} ` `         `  `        ``// Recursively calling selection sort function ` `        ``recurSelectionSort(a, n, index + 1); ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `Main(String []args)  ` `    ``{ ` `        ``int` `[]arr = {3, 1, 5, 2, 7, 0}; ` `     `  `        ``// Calling function ` `        ``recurSelectionSort(arr, arr.Length, 0); ` `     `  `        ``//printing sorted array ` `        ``for` `(``int` `i = 0; i< arr.Length; i++) ` `            ``Console.Write(arr[i] + ``" "``); ` `    ``} ` `}  ` ` `  `// This code is contributed by Princi Singh `

Output:

```0 1 2 3 5 7
```

This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Improved By : princi singh

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.