Im wondering could you help me to solve this questionand you

I\'m wondering could you help me to solve this question?and you don\'t have to write the whole code with main function ?it is enough
to make the following function valid combined with the given hint ?1.divide-and-conquer type algorithm similar to quick sort 2.Choose A[0] as the
Pivot you cannot omit these)Anyway?thank you so much?
9. (10 pts) Consider a problem of finding the k-th smallest element from vector A. Complete the following function. Hint Think of a divide-and-conquer type algorithm similar to quick sort. You may simply choose A[0] as the pivot. float kthsmailest (const vectorsfloat:t A, int k)

Solution


float kthSmallest(const vector<float>& A,int k)
{
   int len = A.size();
   int pvt,hat,temp;
   float ans=FLT_MAX;
   int i,j;
   for(int l=0,r=len-1;k>0&k<=r-l+1;)
   {
       hat = arr[r];
       i=l;
       for(j=l;j<=r-l;j++)
       {
           if(A[j]<=hat)
           {
               temp = A[i];
               A[i] = A[j];
               A[j] = temp;
               i++;
           }
       }
       temp = A[i];
       A[i] = A[r];
       A[r] = temp;
       pvt=i;
       if(pvt-l==k-1)
       {
           ans = A[pvt];
           break;
       }
       if(pvt-l>k-1)
       {
           r=pvt-1;
           continue;
       }
       l = pvt+1;
       k = k-pvt+l-1;
   }
   return ans;
}

I\'m wondering could you help me to solve this question?and you don\'t have to write the whole code with main function ?it is enough to make the following funct

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site