Question: 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 < size; x++) { readFile >> 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 < size; x++) { outputFile << list[x] << " "; } outputFile << endl; // deallocation delete[] list; } // closing our file streams readFile.close(); outputFile.close(); 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 Databases Questions!