Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

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 12.17.1: .
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 (exclusive).(For the median, use k = n/2, a =0, and b = n.)
select(k, a, b)
Pick a pivot p in the subsequence between a and b.
Partition the subsequence elements into three subsequences: the elements p
Let n1, n2, n3 be the sizes of each of these subsequences.
if k < n1
return select(k,0, n1).
else if (k > n1+ n2)
return select(k, n1+ n2, 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 select(int k, int a, int b)
{
int n1=a, n2=b;
int n3= n1+n2;
int p=0;
if(kt
{
return select(k,0, n1);
p++;
}
else if (k > n1+ n2)
{
return select(k, n1+ n2, n3);
p++;
}
else
{
return p;
}
}
int main(){
srand(time(0));
int n =(rand()%10);
int k = n/2;
int a =0;
int b = n;
int jack = select(k, a, b);
cout << n<

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students explore these related Databases questions

Question

What is job rotation ?

Answered: 3 weeks ago