1 1 vote Algorithms algorithms sorting merge-sort time-complexity + – Parshu gate 1.5k views answer comment Share Follow Print See all 6 Comments 6 6 Comments reply joshi_nitish commented Nov 20, 2017 i edited by joshi_nitish Nov 20, 2017 reply Follow flag //i am writing just a pseudocode k=n mrgshort(n) { if(n<=k/2^r) O(n^2); else { mrgshort(n/2); mrgshort(n/2); O(n); } } now, you can easily see if() part is approximately contributing to O(n2) and, else{ } part is contributing about O(nlogn), so overall time complexity = O(n2) PS: understanding the time complexity of above code is little triky but if you will analyze carefully, you will find what i have said. 3 3 replyShare Parshu gate commented Nov 20, 2017 reply Follow flag can u please explain how did u come up with the if() part? 0 0 replyShare Surajit commented Nov 20, 2017 reply Follow flag it goes till r th level,then we do insertion sort and merge back right?. But at rth level the size of each sublist will be r, that means insertion sort would contribute r^2 (for each sublist) * 2^r (no of sublists) and then merge back,how can it be as large as Theta (n^2) ?? The options should have in the order of constants.Imagine the following, n = 256. Sublist size initial = 256 1st recursion = 128,128 2nd recursion = 64,64,64,64 3rd recursion = 16,16,16,16 16,16,16,16 16,16,16,16 16,16,16,16 Suppose we stop now ,notice we have size 16 sublists of size 16. Which if we use insertion sort will take cost as 16^2 * 16 .Then we merge back.How can it be as large as n^2 ??..Then why to use merge sort ,use direct insertion sort in the intial list itself. 1 1 replyShare joshi_nitish commented Nov 20, 2017 reply Follow flag @Surajit But at rth level the size of each sublist will be r, that means insertion sort would contribute r^2 (for each sublist) * 2^r (no of sublists) and then merge back at rth level size of each list will be $\frac{n}{2^{r}}$ and we have total of 2r list, therefore total time taken to perform insertion sort on 2r lists will be 2r * ($\frac{n}{2^{r}}$)2 = O(n2) 3 3 replyShare Surajit commented Nov 20, 2017 reply Follow flag yes correct,my mistake in calculation.But this is kind of increases complexity of the program itself,asymptotically a direct insertion sort might have produced same result maybe added few more constant terms. 0 0 replyShare Surajit commented Nov 20, 2017 reply Follow flag If one can get a sublist of size of 2^r and no of sublist as n/2^r then I think we can perform better.(Just a thought,out of the context of the questions given). 0 0 replyShare Please log in or register to add a comment.