The Selection Sort algorithm sorts maintain two parts.
- The first part that is already sorted
- The second part is yet to be sorted.
The algorithm works by repeatedly finding the minimum element (considering ascending order) from the unsorted part and putting it at the end of the 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 Iterative Selection Sort. In this article recursive approach is discussed. The idea of a recursive solution is to one by one increment sorted part and recursively call for the remaining (yet to be sorted) part.
C++
#include <iostream>
using namespace std;
int minIndex( int a[], int i, int j)
{
if (i == j)
return i;
int k = minIndex(a, i + 1, j);
return (a[i] < a[k])? i : k;
}
void recurSelectionSort( int a[], int n, int index = 0)
{
if (index == n)
return ;
int k = minIndex(a, index, n-1);
if (k != index)
swap(a[k], a[index]);
recurSelectionSort(a, n, index + 1);
}
int main()
{
int arr[] = {3, 1, 5, 2, 7, 0};
int n = sizeof (arr)/ sizeof (arr[0]);
recurSelectionSort(arr, n);
for ( int i = 0; i<n ; i++)
cout << arr[i] << " " ;
cout << endl;
return 0;
}
|
Java
class Test
{
static int minIndex( int a[], int i, int j)
{
if (i == j)
return i;
int k = minIndex(a, i + 1 , j);
return (a[i] < a[k])? i : k;
}
static void recurSelectionSort( int a[], int n, int index)
{
if (index == n)
return ;
int k = minIndex(a, index, n- 1 );
if (k != index){
int temp = a[k];
a[k] = a[index];
a[index] = temp;
}
recurSelectionSort(a, n, index + 1 );
}
public static void main(String args[])
{
int arr[] = { 3 , 1 , 5 , 2 , 7 , 0 };
recurSelectionSort(arr, arr.length, 0 );
for ( int i = 0 ; i< arr.length; i++)
System.out.print(arr[i] + " " );
}
}
|
Python3
def minIndex( a , i , j ):
if i = = j:
return i
k = minIndex(a, i + 1 , j)
return (i if a[i] < a[k] else k)
def recurSelectionSort(a, n, index = 0 ):
if index = = n:
return - 1
k = minIndex(a, index, n - 1 )
if k ! = index:
a[k], a[index] = a[index], a[k]
recurSelectionSort(a, n, index + 1 )
arr = [ 3 , 1 , 5 , 2 , 7 , 0 ]
n = len (arr)
recurSelectionSort(arr, n)
for i in arr:
print (i, end = ' ' )
|
C#
using System;
class GFG
{
static int minIndex( int []a, int i, int j)
{
if (i == j)
return i;
int k = minIndex(a, i + 1, j);
return (a[i] < a[k])? i : k;
}
static void recurSelectionSort( int []a, int n,
int index)
{
if (index == n)
return ;
int k = minIndex(a, index, n - 1);
if (k != index)
{
int temp = a[k];
a[k] = a[index];
a[index] = temp;
}
recurSelectionSort(a, n, index + 1);
}
public static void Main(String []args)
{
int []arr = {3, 1, 5, 2, 7, 0};
recurSelectionSort(arr, arr.Length, 0);
for ( int i = 0; i< arr.Length; i++)
Console.Write(arr[i] + " " );
}
}
|
Javascript
function minIndex(a, i, j) {
if (i == j)
return i;
var k = minIndex(a, i + 1, j);
return (a[i] < a[k])? i : k;
}
function recurSelectionSort(a, n, index = 0) {
if (index == n)
return ;
var k = minIndex(a, index, n-1);
if (k != index)
[a[k], a[index]] = [a[index], a[k]];
recurSelectionSort(a, n, index + 1);
}
var arr = [3, 1, 5, 2, 7, 0];
var n = arr.length;
recurSelectionSort(arr, n);
console.log(arr.join( ' ' ));
|
Output:
0 1 2 3 5 7
Time Complexity: O(n2)
Auxiliary Space: O(n)
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 review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.