# Recursive Selection Sort in C++

Selection Sort is one of the sorting algorithms used to sort data by iterating an array from the beginning and replacing each element with the smallest element in the list. As we move forward the left array is sorted, and the right array is unsorted. Each move places the next smallest to the current position of the index by swapping.

## Selection Sort Algorithm

• int arr[5]= { 5,4,2,1,3 };

• int i, j ;

• Traverse from index i=0 to i<array size -1

• Traverse from index j=i+1 to array size - 1

• find smallest and store its index.pos

• Swap element at found index pos with arr[i]

• End

## Recursive Selection Sort

• Find minimum element’s index

• If the smallest element index found is equal to array size, then return.

• Otherwise swap current element with smallest element

• Recursively perform above steps for rest of array excluding sorted elements

## Examples

Input − Arr[] = { 5,7,2,3,1,4 }; length=6

Output − Sorted array: 1 2 3 4 5 7

Explanation

First Pass :-
5 7 2 3 1 4 → swap → 1 2 7 3 5 4
1 2 7 3 5 4 → no swap
1 2 7 3 5 4 → swap → 1 2 3 7 5 4
1 2 3 7 5 4 → swap → 1 2 3 4 5 7
1 2 3 4 5 7 → no swap

Input − Arr[] = { 1, 2, 3, 3, 2 };

Output − Sorted array: 1 2 2 3 3

Explanation

1 2 3 3 2 → no swap
1 2 3 2 3 → no swap
1 2 3 2 3 → swap → 1 2 2 3 3
1 2 2 3 3 → no swap

## Approach used in the below program is as follows

In the recursive approach of Selection sort, the base case is minimum index = array size-1. Otherwise find minimum from the array swap with current index and recursively sort right unsorted array.

• Take the input array Arr[] and length as the number of elements in it.

• Function findMin(int arr[], int i, int j) takes the array and its indexes and returns the index of minimum element between arr[i+1] to arr[j].

• Take a variable minpos.

• If both i and j are the same, then return i as the index of the minimum element as both are the same.

• Otherwise recursivley look for position i+1 to j using minpos = findMin(arr, i + 1, j).

• if(arr[i]<arr[minpos]) then set { minpos=i;} and return minpos.

• Function recurselectSort(int arr1[], int len1, int pos1) takes input array and sorts it in ascending order using recursion in selection sort.

• If pos1 == len1 then no minimum found, then return.

• Else set minpos1 = findMin(arr1, pos1, len1-1)

• If the current pos1 index and minimum element index minpos1 are not the same, then swap elements in these indexes using temp.

• Recurse for the rest of the array using recurselectSort(arr1, len1, pos1 + 1).

• At the end of all calls, when len becomes 1 we will come out of recursion and the array will be sorted.

• Print the sorted array inside main.

## Example

#include <iostream>
using namespace std;
int findMin(int arr[], int i, int j){
int minpos;
if (i == j){
return i;
}
minpos = findMin(arr, i + 1, j);
if(arr[i]<arr[minpos]){
minpos=i;
}
return (minpos);
}
void recurselectSort(int arr1[], int len1, int pos1){
int temp;
int minpos1;
if (pos1 == len1){
return;
}
minpos1 = findMin(arr1, pos1, len1-1);
if (minpos1 != pos1){
temp=arr1[pos1];
arr1[pos1]=arr1[minpos1];
arr1[minpos1]=temp;
}
recurselectSort(arr1, len1, pos1 + 1);
}
int main(){
int Arr[] = {1,5,3,0,9,3,5};
int length = sizeof(Arr)/sizeof(Arr[0]);
recurselectSort(Arr,length,0);
cout<<"Sorted Array using recursive Selection sort: "<<endl;
for (int i = 0; i<length ; i++){
cout << Arr[i] << " ";
}
return 0;
}

## Output

If we run the above code it will generate the following Output

Sorted Array using recursive Selection sort:
0 1 3 3 5 5 9