# Combining merge sort and insertion sort

A cache-aware sorting algorithm sorts an array of size $2^{k}$ with each key of size 4 bytes. The size of the cache memory is 128 bytes and algorithm is the combinations of merge sort and insertion sort to exploit the locality of reference for the cache memory (i.e. will use insertion sort when problem size equals cache memory).

What are the best case and worst case running time of the algorithm?

• You should ask a more specific question. Requesting a general description how to determine the runtime of algorithms is too broad. Which parts of the text did you not follow? – adrianN Jan 2 '17 at 14:41
• See , given this problem I coundnt understand how to make an approach to this . I cound't make it short than this . It is fine if someone could explain it using Merge and Insertion sort . I will try to figure out a general approach from it. But is first place I need to know how to approach to this question – user63554 Jan 2 '17 at 14:51
• Possible duplicate of Is there a system behind the magic of algorithm analysis? – David Richerby Jan 2 '17 at 16:47
• Actually Not ! This is a specific question.Please take some effort to read it once ! Thanks :) – user63554 Jan 2 '17 at 17:16
• What did you try? Where did you get stuck? We're happy to help you understand the concepts but just solving exercises for you is unlikely to achieve that. You might find this page helpful in improving your question. – D.W. Jan 3 '17 at 1:51

Basic Idea

• For small values of n insertion sort runs faster than merge sort . Hence insertion sort can be used to Optimize merge sort

• Basic idea is apply insertion sort on sublists obtained in merge sort and merge the sorted (using insertion sort) lists

Coming to Question

Base Condition

If each word is 4-byte long, then a 128-byte cache contains 32 words. Therefore, if the problem size is 32 or less, the instance will be immediately solved by Insertion Sort, otherwise it will be split in two sub-problems with the same size, which will then have to be merged

Time Complexity

Total Complexity = Complexity Insertion Sort + Complexity of Merge Sort

We have $n$ elements . We are dividing it to $\frac{n}{x}$ sublist each contain x elements each Complexity Of Merge Sort

• Total work done by merge sort in copying $n$ elements from each level to upper level is $O(n)$
• Number of levels = number of sublists or small problem = $O(\log \frac{n}{x})$
• Complexity of Merge sort : $O( n \log \frac{n}{x} )$

Complexity Of Insertion Sort

• We are sorting $\frac{n}{x}$ lists each of $x$ elements using insertion sort so complexity contributed by insertion sort is $\frac{n}{x}$ INSERTION ${x}$

Total Complexity = $\Theta ( \frac{n}{x} \ \text{INSERTION} \ (x) + n \log \frac{n}{x} )$

Best Case

• is when Insertion sort perform in best case with $O(n)$ ie $\text{INSERTION} \ (x) = x$
• Best Case Complexity=Total Complexity = $\Theta ( n + n \log \frac{n}{x} )$

Worst Case

• is when Insertion sort perform in best case with $O(n^{2})$ ie $\text{INSERTION} \ (x) = x^{2}$
• Best Case Complexity=Total Complexity = $\Theta ( nx + n \log \frac{n}{x} )$

I will only consider worst case, leaving best case to you. The running time $T(n)$ of merge sort on an instance of length $n$ (words) satisfies the recurrence relation $$T(n) = 2T(n/2) + O(n),$$ with a base case of $T(1) = O(1)$. In your case, the base case is different: $T(32)$ is the running time of insertion sort on an array of length $32$. Since 32 is constant, we can write $T(32) = O(1)$, and then solve the recurrence relation as in pure merge sort. The result will be exactly the same - the difference between the algorithms (pure merge sort and modified merge sort as in your question) is hidden in the big O constant.