Question
1 Merge Sort and Insertion Sort Although merge sort runs in (n lg n) worst-case time and insertion sort runs in (n 2 ) worst-case
1 Merge Sort and Insertion Sort Although merge sort runs in (n lg n) worst-case time and insertion sort runs in (n 2 ) worst-case time, the constant factors in insertion sort can make it faster in practice for small problem sizes on many machines. Thus, it makes sense to coarsen the leaves of the recursion by using insertion sort within merge sort when subproblems become sufficiently small. 1.1 Experiment: Merge Sort vs. Insertion Sort The goal of this first experiment is to compare empirically time complexities of Insertion sort vs. Merge sort. To do so, after coding these two algorithms, you will need to calculate the time com- plexities of the two algorithms for different values of n and plot the obtained complexity values. The time complexities will be approximated only by counting the numbers of tests (like : if (A[i] > 1), and simple instructions (like: A[i] = A[i 1] + 1). To draw plots, you will need to calculate time complexities for different values of n. For this consider the following ones: 5, 10, 15, ... , 90, 95, 100. For each of these values, you will need to generate an array of random integer values between 0 and 1000, which size is equal to the value of n. To avoid the effect of sampling you will need to repeat the calculations for 10 different arrays of the same size and find the time complexity for the value of n as the average of the ten calculated complexity values. Required Work 1 provide the code for both algorithms and show your counters used for calculating time complexity. 2. Plot the time complexity graphs for both algorithms on the same figure. 3. What is the biggest value of n after which merge sort is better than insertion sort?
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