Please use C 11 Define a kelement partial sort of a vector v

Please use C++ 11.

Define a k-element partial sort of a vector v, where 0 < k < v.size(), to be a permutation (i.e., rearrangement) of the elements of v in which: the smallest value is placed in v[0]; the second-smallest value is placed in v[1]; and so on until the k-th smallest value is placed in v[k-1]; and the remaining values are stored in the remaining elements of v without any ordering restrictions. For example, if v is a 9-element vector with the values 3 0 19 77 -4 11 22 13 2, then:

• -4 0 19 77 3 11 22 13 2 and -4 0 77 11 3 19 2 13 22 are both 2-element partial sorts of v, and

• -4 0 2 3 11 19 13 77 22 is one possible 5-element partial sort of v.

a.State the invariant condition on the array elements at the end of the ith loop in Selection Sort.

b.Modify Selection Sort to become an O(n· k) algorithm for solving the k-element partial sorting problem. Write your solution in the space below. [HINT: How many iterations do you need for this new problem?] template void k_select(vector &v, int k) {

Solution

# include <iostream.h>
# include <conio.h>
class sorting
{
int a[100],n;
public:
void accept()
{
cout<<\"ENTER N VALUE \";
cin>>n;

   for(int i=0;i<n;i++)
   {
   cout<<\"ENTER \"<<i<<\" Value \";
   cin>>a[i];
   }
}
void print()
{
   for(int i=0;i<n;i++)
   cout<<a[i]<<endl;
}
void sort()
{
   int i,j,tmp;
   for(i=0;i<n;i++)
   for(j=i+1;j<n;j++)
   if(a[j]<a[i])
   {
   tmp=a[i];
   a[i]=a[j];
   a[j]=tmp;
   }
}
};
void main()
{
sorting s;
s.accept();
cout<<\"The values before sortig \"<<endl;
s.print();
s.sort();
cout<<\"The values after sorting \"<<endl;
s.print();
getch();
}

Please use C++ 11. Define a k-element partial sort of a vector v, where 0 < k < v.size(), to be a permutation (i.e., rearrangement) of the elements of v i
Please use C++ 11. Define a k-element partial sort of a vector v, where 0 < k < v.size(), to be a permutation (i.e., rearrangement) of the elements of v i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site