Question: Can you please review this program function, method or class that will track the true runtime of your algorithm. Find the true runtime of your
Can you please review this program function, method or class that will track the true runtime of your algorithm. Find the true runtime of your algorithm using arrays of varying sizes (e.g., n = 500, n = 1,500, and n= 2,500) using your new tool. Plot, on a Cartesian plane, the runtime of your algorithm as a function of the size of the input array, n.
#include
// Function to reverse the order of elements in an array void reverseArray(int arr[], int size) { for (int i = 0; i < size/2; i++) { // Swap the i-th element from the start with the i-th element from the end int temp = arr[i]; arr[i] = arr[size-1-i]; arr[size-1-i] = temp; } }
// Function to time the execution of the reverseArray function double timeFunction(int arr[], int size) { auto start = std::chrono::high_resolution_clock::now(); reverseArray(arr, size); auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration
int main() { int n1 = 500; int n2 = 1500; int n3 = 2500;
// arrays of size n1, n2, and n3 int arr1[n1], arr2[n2], arr3[n3]; for (int i = 0; i < n1; i++) { arr1[i] = i; } for (int i = 0; i < n2; i++) { arr2[i] = i; } for (int i = 0; i < n3; i++) { arr3[i] = i; }
// Time the execution of the reverseArray function for each array double time1 = timeFunction(arr1, n1); double time2 = timeFunction(arr2, n2); double time3 = timeFunction(arr3, n3);
// Print the execution times std::cout << "Execution time for array of size " << n1 << ": " << time1 << " seconds" << std::endl; std::cout << "Execution time for array of size " << n2 << ": " << time2 << " seconds" << std::endl; std::cout << "Execution time for array of size " << n3 << ": " << time3 << " seconds" << std::endl;
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
