Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Help Thank you! Write a recursive function to sort an array of integers into

ID: 3574619 • Letter: C

Question

C++ Help Thank you!

Write a recursive function to sort an array of integers into ascending order using the following idea: place the smallest element in the first position, then sort the rest of the array by a recursive call. This is a recursive version of the selection sort. (Note: You will probably want to call an auxiliary function that finds the index of the smallest item in the array. Make sure that the sorting function itself is recursive. Any auxiliary function that you use may be either recursive or iterative.) Embed your sort function in a driver program to test it. Turn in the entire program and the output.

Explanation / Answer

#include <iostream>
using namespace std;
int min(int a[],int st,int ed)
{
   int mn = st;
   for(int i=st+1;i<ed;i++)
   {
       if(a[i]<a[mn])
       {
           mn=i;
       }
   }

   return mn;
}
void sort(int a[],int st,int ed)
{
   if(st==ed)
   {
       return;
   }
   int mn = min(a,st,ed);
   int temp = a[mn];
   a[mn]=a[st];
   a[st]=temp;
   sort(a,st+1,ed);
}
int main()
{
   int a[]= {5,3,7,1,9,10,0,3};
   sort(a,0,8);
   for(int i=0;i<8;i++)
   {
       cout<<a[i]<<' ';
   }
   cout<<endl;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote