Thank you for your helpful answerIts necessary for you to pa

Thank you for your helpful answer!(It\'s necessary for you to pay attention to the given information)
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

#include <stdio.h>
#include<conio.h>
#include<iostream.h>
#include<vector>
int kth_smallest(vector<float>& vec, int k)
{
int minIndex, minValue, tmp;
for (int i = 0; i < vec.size(); i++)
{

for (int j = i+1; j < vec.size(); j++)
{
if (vec[i] > vec[j])
{
tmp=vec[j];
vec[j]=vec[i];
vec[i]=tmp;
}
}

}

return vec[k-1];
}

int main()
{
vector<float> v;
v.push_back(10);
v.push_back(1);
v.push_back(12);
v.push_back(9);
v.push_back(3);
v.push_back(7);
cout <<\"2nd smallest element is \"<<kth_smallest(v,2)<< endl;
  
}

Thank you for your helpful answer!(It\'s necessary for you to pay attention to the given information) 9. (10 pts) Consider a problem of finding the k-th smalles

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site