Question: Compare execution times of Insertion sort Function by varying the input size of arrays. Vary the input size of arrays using 10 , 100, 1000,
#include using namespace std; void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i { key = arr[i]; j = i - 1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } /* Driver code */ int main() { time_t start, end; int n=100000; int arr[n]; for(int i = 0; i;> { arr[i] =rand(); } time(&start); // unsync the I/O of C and C++. ios_base::sync_with_stdio(false); insertionSort(arr, n); // Recording end time. time(&end); for(int i = 0; i;> { cout } // Calculating total time taken by the program. double time_taken = double(end - start); cout cout return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
