Find the bestcase running time of MergeSort Show that MergeS
Find the best-case running time of MergeSort? Show that MergeSort can be modified such that (a) the worst-case running time is the same, and (b) the best-case running time is O(n).
To sort SIO..n 1) call Merge Sort(S, n) RecurSort (l, r) Mergesort (S, n) Input: array S l..] Input: array SIO..n 1] of n numbers Output: sorted numbers in S l..r Output: sorted numbers in Sl..r] 1 if (l r) 1 create tempo array X10..n -10 2 m (l r)/2; middle element 2 RecurSort (0, n 1); 3 Recur Sort(l, m); assume that SI..l and XI.. 4 Hecursort(m 1, r); are available in RecurSort 5 Merge (l, m, r); Merge(l, m, r Input: array SIl..r] such that SEl..ma] and Sm 1..r] are sorted. Output: sorted numbers in Sl..r 1 create temp array XLl..rl or pass it as a parameter 2 il l: i2 m 1; the first elements of the two subarrays for i i to r 4 if (il S m and (i2 r or Sli1] Sli2))) condition for copying Sli1] else 8 Copy XI..r] to SIl..r]Solution
Ans. Best case running time of MergeSort: O(n log(n))
We can use natural variant technique to get the time complexity as O(n). It is defined as below:
It exploits naturally occurring runs. It takes the already sorted part and appends it to any of the naturally occurring runs.
In the best case, naturally occurring run is the other sorted part which will be executed in O(n) time.
