Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Using C++ Modify the code to collect running time data. Call the new timing program badStoogeSort. Instead of reading arrays from the file data.txt and
Using C++ Modify the code to collect running time data. Call the new timing program badStoogeSort. Instead of reading arrays from the file data.txt and sorting, you will now generate arrays of size n containing random integer values from 0 to 10,000 to sort. Use the system clock to record the running times of each algorithm for n = 5000, 10000, 15000, 20,000, .... for two values of = 2/3 and = 3/4. You may need to modify the values of n if an algorithm runs too fast or too slow #include#include #include #include using namespace std; /* our base case for the sorting will be when there are only 2 elements if we meet that condition we compare the 2 elements and perform a swap operation if needed else we perform a recursive call on 2/3 of the size of the array we are creating 3 sub problems 1 focuses on the initial 2/3rd of our data the other will focus on the other 2/3rd of our data Another way to visualize it is we analyze 2/3rd from our starting point first we them analyze 2/3rd from our end point our last recursive call is to analyze the initial 2/3rd again we continue until we only have 2 elements than we swap if needed and trace back to the calls still follows the divide and conquer rules. we divide into 2/3rds we conquer by swapping first and last elements of a subproblems */ void stoogeSort(int arr[], int start, int end) { int size = end - start+1; // base case when there are only 2 elements if (size == 2) { // condition to check if we need to perform a swap operation if (arr[start] > arr[end]) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; } } else if (size > 2) { // here we get the index value that represents our focal point // you can view this as our midpoint but instead of half we are focused with what index falls at 2/3rds of our array int point = size/3; // we recursively sort the first 2/3rd stoogeSort(arr, start, end - point); // here we do the second 2/3rd stoogeSort(arr, start+point, end); // we perform another recursion on the first 2/3rd again to verify // this repeated call is in the case if there was a swap that took place in the second call stoogeSort(arr, start, end - point); } } int main() { ifstream readFile("data.txt"); ofstream outputFile("stooge.out"); while (!readFile.eof()) { int size; int* list; // getting the number of elements readFile >> size; // allocating enough room for our dynamic array list = new int[size]; // getting the elements for our dynamic array int temp; for (int x = 0; x > temp; list[x] = temp; } // Sorting our values stoogeSort(list, 0, size-1); // writing the result to the insert.out file for (int x = 0; x Consider the following pseudocode for a sorting algorithm, for 0 1. badSort(A[O...n - 1]) if (n = 2) and (A[0] > A[1]) swap A[0] and A[1] else if (n > 2) m = fa.nl badSort(A[O...m 1]) badSort(A[n m...n 1]) badSort(A[O...m 1])
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started