Question: quickselect.cpp * Author : * Pledge : * Date : * Description : Implements the quickselect algorithm. * * * * * * * *

quickselect.cpp
* Author :
* Pledge :
* Date :
* Description : Implements the quickselect algorithm.
******************************************************************************/
#include
#include
#include
#include
using namespace std;
size_t lomuto_partition(int array[], size_t left, size_t right){
// TODO
// DO NOT change the function header in any way, otherwise you will lose
points.
return 0;
}
int quick_select(int array[], size_t left, size_t right, size_t k){
// TODO
// DO NOT change the function header in any way, otherwise you will lose
points.
return 0;
}
int quick_select(int array[], const size_t length, size_t k){
return quick_select(array,0, length -1, k);
}
int main(int argc, char *argv[]){
if (argc !=2){
cerr << "Usage: "<< argv[0]<<""<< endl;
return 1;
}
int k;
istringstream iss;
iss.str(argv[1]);
if (!(iss >> k)|| k <=0){
cerr << "Error: Invalid value '"<< argv[1]<<"' for k."<< endl;
return 1;
}
cout << "Enter sequence of integers, each followed by a space: "<< flush;
int value, index =0;
vector values;
string str;
str.reserve(11);
char c;
iss.clear();
while (true){
c = getchar();
const bool eoln = c =='\r'|| c =='
';
if (isspace(c)|| eoln){
if (str.length()>0){
iss.str(str);
if (iss >> value){
values.push_back(value);
} else {
cerr << "Error: Non-integer value '"<< str
<<"' received at index "<< index <<"."<< endl;
return 1;
}
iss.clear();
++index;
}
if (eoln){
break;
}
str.clear();
} else {
str += c;
}
}
int num_values = values.size();
if (num_values ==0){
cerr << "Error: Sequence of integers not received." << endl;
return 1;
}
// TODO - error checking k against the size of the input
// TODO - call the quick_select function and display the result
return 0;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!