Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are to use the parse function from the previous lab to parse an input delimited text string to tokens in a vector. Additional vector

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

You are to use the parse function from the previous lab to parse an input delimited text string to tokens in a vector. Additional vector programming examples can be found in cplusplus.com (Links to an external site.)Links to an external site..

The Starter:

Use the quicksort_starter.cpp from github.

The user is expected to enter the entire collection (of test data) within one single line of delimited data.

quicksort.cpp and appropriate validations.

As an example, the program should be able to produce the trace output when the TRACE option is enabled. Here is an example output:

Enter your choice> s (9 7 3 5 6 8 2) (2 7 3 5 6 8 9) (2 3)5 8 6 7 9 2 3 5 (8 6 7 9) 2 3 5 (6 8 7 9) 2 3 5 (6 7)8 9 2 3 5 6 7 8 (9) Sorted Array: 2,3,5,6,7,8,9 

quick_sort.cpp

#include

#include

#include // container

#include // menu

#include // qsort

#define under "\33[4m"

#define over "\33[0m"

// example cout

using namespace std;

static const int MIN_SIZE = 2; //smaller use swap

char delimiter = ',';

bool trace = false;

void showTrace(vector& arr, int first, int last, int pivotIndex);

void show(vector& arr);

int comp(const void *one, const void *two);

void order(vector& arr, int i, int j);

void sortFirstMiddleLast(vector& arr, int first, int last);

int partition(vector& arr, int first, int last);

void quickSort(vector& arr, int first, int last);

int main() {

cout

vector a={9,7,3,5,6,8,2};

cout

for(auto item:a) cout

cout

// Try lambda for local meaning

auto acomp = [] (const void* a, const void* b)

{return ( (a > b)?1:( (a==b)?0:-1) );};

vector demo(a);

qsort(&demo[0], demo.size(), sizeof(int), acomp);

cout

for(auto item:demo) {

cout

}

cout

vector ar = {"9","7","3","5","6","8","2"};

auto menu = [&] () {

cout

show(ar);

cout

menu( );

char choice; // user choice of command

do {

cout

cin >> choice;

cin.ignore(1000, ' ');

choice = tolower(choice);

string input, token;

vector test(ar);

stringstream ss;

switch(choice) { // main menu switch starts

case 'n': // new

cout

getline (cin, input);

ss

ar.clear();

while(getline(ss, token, delimiter))

ar.push_back(token);

break;

case 't':

trace = !trace;

break;

case 'd': // delimiter

cout

cin >> delimiter;

break;

case 's':

// quickSort(test, 0, test.size() - 1);

cout

show(test);

cout

break;

case 'm':

menu();

break;

case 'q':

break;

default:

cout

}

} while (choice!='q');

}

// void quickSort(vector& arr, int first, int last)

// {

// }

void sortFirstMiddleLast(vector& arr, int first, int last) {

int middle = (first + last)/2;

order(arr, first, middle);

order(arr, middle, last);

order(arr, first, middle);

}

// int partition(vector& arr, int first, int last){

// }

int comp(const void *one, const void *two) {

int a = *((int*)one);

int b = *((int*)two);

if (a

if (a == b) return 0;

return 1;

}

void show(vector& arr) {

string output; // how does this one work?

for(auto item:arr) {

if (!output.empty()) output += ",";

output += item;

}

cout

}

void showTrace(vector &arr, int first, int last, int p) {

for(int i=0; i

cout

}

cout

}

void order(vector& arr, int i, int j) {

if (arr[i] > arr[j])

std::swap(arr[i], arr[j]);

}

Current output:

image text in transcribed

This assignment is to write a quicksort.cpp program based on the Pseudo Code Example on chapter 11.2.2, p.318- 324. You are to implement a Quick Sort program based on the textbook example. This program have to trace the sorting of a random array in progress and its trace output needs to Show all items of the array on a single trace line Each data exchange needs to be on a separate trace line, and . Show the active sorting range by placing an opening and a closing parenthesis around it. Your program needs to run the quick sort only, i,e. sort the data using quick sort all the way through, from the begining to the end, Do not swtich to Insertion Sort as described in the textbook Although the Quick sort pivot selection is costly especially when the item numbers to sort is small. Therefore, the textbook example switch to selection sort when the item numbers in the segment to sort is less than 10. In this assignment, our main objective is to learn how this sort works, hence we are not going to fine tune the performance by switching to another basic sort, cheaper to setup when the item numbers in the segment to sort is below a certain threshold Here are the features need to be in your quick sort program: Write C++ program based on this pseudo code in the textbook; .A the program start, show a predefined list (of your own) than before; Use the median-of-three (fist middle last) pivot selection. Do NOT switch to a basic (selection) sort when the item numbers in the segment to sort is smaller than the threshold! Stop the Quick Sort when the terminal case is reached, i.e. the segment to be sorted is less or equal to 2 items. If the number is 2, perform a simple swap if out of order. Otherwise, just return. Display (prompt) the menu, defined in the User Interface below, after the start test and after the completion of a menu command Explain what and why for the worst case and average case of quick sort efficiency in Big O notation on the header of the quicksort.cpp

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

Step: 3

blur-text-image

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

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

c. What were the reasons for their move? Did they come voluntarily?

Answered: 1 week ago

Question

5. How do economic situations affect intergroup relations?

Answered: 1 week ago