Question
Although merge sort runs in O(nlgn) worst-case time and insertion sort runs in O(n 2 ) worst-case time, the constant factors in insertion sort make
Although merge sort runs in O(nlgn) worst-case time and insertion sort runs in O(n2 ) worst-case time, the constant factors in insertion sort make it faster for small n. So, it makes sense to use insertion sort within merge sort when sub problems are small enough. (For example, recursion can stop when lists have length k and insertion sort can run to sort these smaller lists.) Consider a modifcation to merge sort in which n/k sublists of length k are sorted using insertion sort and then merged using the standard merging mechanism. The problem is then to fnd the appropriate (small enough) value of k.
a. Write pseudo-code for this algorithm showing where in the merge sort code insertion sort would be called.
b. Show that the n/k sublists, each of length k, can be sorted by insertion sort inO(nk) worst-case time.
c. Show that the sublists can be merged in O(nlg(n/k)) worst-case time.
d. Given that the modifed algorithm runs in O(nk + nlg(n/k)) worst-case time, what is the largest asymptotic (O-notation) value of k as a function of n for which the modifed algorithm has the same asymptotic running time as standard merge sort? Hint: Do not solve for k explicitly, do this by inspection.
e. How should k be chosen in practice? Defend your answer.
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