Question: I need help on making the C + + Code Programming Project 1 2 . 1 7 . 1 : . The median m of
I need help on making the C Code
Programming Project :
The median m of a sequence of n elements is the element that would fall in the middle if the sequence was sorted. That is e m for half the elements, and m e for the others. Clearly, one can obtain the median by sorting the sequence, but one can do quite a bit better with the following algorithm that finds the kth element of a sequence between a inclusive and b exclusiveFor the median, use k n a and b n
selectk a b
Pick a pivot p in the subsequence between a and b
Partition the subsequence elements into three subsequences: the elements p
Let n n n be the sizes of each of these subsequences.
if k n
return selectk n
else if k n n
return selectk n n n
else
return p
Implement this algorithm and measure how much faster it is for computing the median of a random large sequence, when compared to sorting the sequence and taking the middle element.
#include
#include
#include
#include
using namespace std;
int selectint k int a int b
int na nb;
int n nn;
int p;
ifkt
return selectk n;
p;
else if k n n
return selectk n n n;
p;
else
return p;
int main
srandtime;
int n rand;
int k n;
int a ;
int b n;
int jack selectk a b;
cout n
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
