#include #include #include #include #include #include using namespace std; void merge(string a[], string b[], int left, int mid, int right) { int nL = mid - left + 1; int nR = right - mid; string leftA[nL], rightA[nR], leftB[nL], rightB[nR]; for(int i = 0; i < nL; i++) { leftA[i] = a[nL+i]; leftB[i] = b[nL+i]; } for(int i = 0; i < nR; i++) { rightA[i] = a[mid+i+1]; rightB[i] = b[mid+i+1]; } int i = 0, j = 0, k = left; while(i < nL && j < nR) { if(leftA[i] <= rightA[j]) { a[k] = leftA[i]; b[k] = leftB[i]; i++; } else { a[k] = rightA[j]; b[k] = rightB[j]; j++; k++; } } while(i < nL) { a[k] = leftA[i]; b[k] = leftB[i]; i++; k++; } while(j < nR) { a[k] = rightA[j]; b[k] = rightB[j]; j++; k++; } } void mergeSort(string a[], string b[], int left, int right) { int mid; if(left < right) { mid = left + (right-1)/2; mergeSort(a,b,left,mid); mergeSort(a,b,mid+1,right); merge(a,b,left,mid,right); } } int menu() { int ch; cout<<"1. Time for sorting\n2. Unsorted List\n3. Sort\n4. Exit\nEnter Choice: "; cin>>ch; return ch; } int main() { int n; cout<<"Enter number of order ids: "; cin>>n; srand((unsigned) time(0)); string orderid[n], cost[n], originalorderid[n], originalcost[n]; for(int i = 0; i < n; i++) { int randNum = (rand()%n)+1; orderid[i] = "FD" + to_string(randNum); originalorderid[i] = orderid[i]; float a = 1.0; float randCostNum = (float(rand())/float(RAND_MAX) * a); randCostNum = (int)(randCostNum * 100 + 0.5); randCostNum = (float)randCostNum / 100; cost[i] = "RM" + to_string(randCostNum); originalcost[i] = cost[i]; } time_t start, end; time(&start); ios_base::sync_with_stdio(false); mergeSort(orderid, cost, 0, n-1); time(&end); double timeTaken = double(end - start); bool w = true; while(w) { int choice = menu(); switch(choice) { case 1 : cout << "Time taken by merge sort is : "<< timeTaken << setprecision(5); cout << " sec " << endl; break; case 2 : for(int i = 0; i < n; i++) { cout<

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

#include<iostream>
#include<string>
#include <cstdlib>
#include <ctime>
#include<time.h>
#include <iomanip>
using namespace std;

void merge(string a[], string b[], int left, int mid, int right) {
int nL = mid - left + 1;
int nR = right - mid;
string leftA[nL], rightA[nR], leftB[nL], rightB[nR];
for(int i = 0; i < nL; i++) {
leftA[i] = a[nL+i];
leftB[i] = b[nL+i];
}
for(int i = 0; i < nR; i++) {
rightA[i] = a[mid+i+1];
rightB[i] = b[mid+i+1];
}
int i = 0, j = 0, k = left;
while(i < nL && j < nR) {
if(leftA[i] <= rightA[j]) {
a[k] = leftA[i];
b[k] = leftB[i];
i++;
}
else {
a[k] = rightA[j];
b[k] = rightB[j];
j++;
k++;
}
}
while(i < nL) {
a[k] = leftA[i];
b[k] = leftB[i];
i++;
k++;
}
while(j < nR) {
a[k] = rightA[j];
b[k] = rightB[j];
j++;
k++;
}
}

void mergeSort(string a[], string b[], int left, int right) {
int mid;
if(left < right) {
mid = left + (right-1)/2;
mergeSort(a,b,left,mid);
mergeSort(a,b,mid+1,right);
merge(a,b,left,mid,right);
}
}

int menu() {
int ch;
cout<<"1. Time for sorting\n2. Unsorted List\n3. Sort\n4. Exit\nEnter Choice: ";
cin>>ch;
return ch;
}

int main() {
int n;
cout<<"Enter number of order ids: ";
cin>>n;
srand((unsigned) time(0));
string orderid[n], cost[n], originalorderid[n], originalcost[n];
for(int i = 0; i < n; i++) {
int randNum = (rand()%n)+1;
orderid[i] = "FD" + to_string(randNum);
originalorderid[i] = orderid[i];
float a = 1.0;
float randCostNum = (float(rand())/float(RAND_MAX) * a);
randCostNum = (int)(randCostNum * 100 + 0.5);
randCostNum = (float)randCostNum / 100;
cost[i] = "RM" + to_string(randCostNum);
originalcost[i] = cost[i];
}
time_t start, end;
time(&start);
ios_base::sync_with_stdio(false);
mergeSort(orderid, cost, 0, n-1);
time(&end);
double timeTaken = double(end - start);
bool w = true;
while(w) {
int choice = menu();
switch(choice) {
case 1 : cout << "Time taken by merge sort is : "<< timeTaken << setprecision(5);
cout << " sec " << endl;
break;
case 2 : for(int i = 0; i < n; i++) {
cout<<originalorderid[i]<<"\t\t"<<originalcost[i]<<endl;
}
break;
case 3 : for(int i = 0; i < n; i++) {
cout<<orderid[i]<<"\t\t"<<cost[i]<<endl;
}
break;
case 4 : w = false;
break;
default: w = false;

}
}
return 0;
}

 

Write a pseudocode for your solution above. Do ensure that the pseudocode models the entire logical flow of your program.
d. Illustrate the growth of the algorithm by:
i. providing a table that reports the growth of the sorting algorithm in the
implementation above; and


ii. plot a graph to illustrate the growth of the sorting algorithm.
To illustrate the growth of your sorting algorithm, run the program with at least the following n values (n represents the number of order ids that was generated): 1,000; 5,000; 32,000; 512,000; 1,000,000. The growth of the algorithm is typically measured in millisecond (ms) or microsecond (µs) or nanosecond (ns). The growth rate of your sorting algorithm may vary due to your processor speed, memory capacity and etc. Therefore, to illustrate the consistency of the growth rate of your program, you will need to perform and report a minimum of 5 trial runs on each of the n values. You
may then take the average of the running time for plotting the graph and for the discussion in the section below. 

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY