Question
I am having a hard time seeing how to code this problem. I have posted what I have so far for code. It needs to
I am having a hard time seeing how to code this problem. I have posted what I have so far for code. It needs to be C++ and I am looking at creating a couple functions like "void four_Way_Merge" and "void four_Way Merge_Sort":
a) Implement 4-way Merge sort to sort an array/vector of integers and name it merge4. Implement the algorithm in the C++. Your program should be able to read inputs from a file called data.txt where the first value of each line is the number of integers that need to be sorted, followed by the integers (like in HW 1). The output will be written to a file called merge4.txt.
b) Modify code- Now that you have verified that your code runs correctly using the data.txt input file, you can modify the code to collect running time data. 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 ten different values of n for example: n = 5000, 10000, 15000, 20,000, , 50,000. You may need to modify the values of n if an algorithm runs too fast or too slow to collect the running time data (do not collect times over a minute). Output the array size n and time to the terminal. Name the new program merge4Time.
#include
using namespace std;
void four_Way_Merge(int arr_1[], int size, int size_1, int size_2, int size_3, int arr_2[]){ int one = size; int two = size_1; int three = size_2; int four = size_3
while((one < size_1) && (two < size_2) && (three < size_3)){ if(arr_1[one] < arr_1[two]){ if(arr_1[one] < arr_1[three]){ arr_2[four++] = arr_1[one++]; }else{ arr_2[four++] = arr_1[three++]; } } int main(){ ifstream in("data.txt"); if(in.fail()){ cout << "Error: File did not open" << endl; return 0; }
ofstream out("merge4.txt"); int x;
//finds the size of the array while(in >> x){ int *arr = new int[x]; int j = 0; while(j < x){ in >> arr[j]; j++; }
four_merge_sort(arr, 0, x - 1);
//write to the output file for(int i; i < x; i++){ out << arr[i] << " ";
}
out << endl; }
in.close(); out.close();
cout << "Data has been sorted and placed in the file merge4.txt" << endl;
return 0; }
/*
//Main function for part B of the question int main(){ int n; cout << "Enter size of array to be timed: "; cin >> n;
int arr[n];
time_t start, end;
for(int i = 0; i < n; i++){ arr[i] = rand()%(n + 1); }
cout << endl; cout << "Array before sort: " << endl;
//print array for(int i = 0; i < n; i++){ cout << arr[i] << " "; } time(&start);
four_merge_sort(arr, 0, n - 1);
time(&end);
cout << endl; cout << "Sorted Array" << endl;
for(int i = 0; i < n; i++){ cout << arr[i] << " "; }
cout << endl; cout << "Time used to sort array: " << difftime(end, start) << endl;
return 0; } */
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