text: Recursive Selection Sort | Recursive C ++ program to sort an array using selection sort ; Return minimum index ; Find minimum of remaining elements ; Return minimum of current and remaining . ; Recursive selection sort . n is size of a [ ] and index is index of starting element . ; Return when starting and size are same ; calling minimum index function for minimum index ; Swapping when index nd minimum index are not same ; swap ; Recursively calling selection sort function ; Driver code ; Calling function ; printing sorted array
code: #include <iostream> NEW_LINE 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 ; }