5,627 views
1 1 vote
In Merge sort Algorithm when I took input array of size 2 and I got 4 function calls as including original function call with which I call MS algorithm i.e. MS (1,2) and which in turn calls two recursive function calls to merge sort as MS (1,1) and MS (2,2) and one function call to merge procedure as Merge (1,1,2)


Likewise for input array of size 3 I got 7 function calls.

for input array of size 6 I got 16 function calls.
So, how can I analyze the total number of function calls when input array size is n?


thank you!

4 Answers

4 4 votes
For Merge Sort

no of times Merge Sort called = $2n - 1$

no of times merge is called = $n-1$
2 2 votes
For ‘Merge-sort’ the number of function calls can be represented by T(n)

$T(n)=2T(\frac{n}{2})+2$, and T(2)=2

Solving which we get 2n-2, we add 1 for call from the main function, therefore (2n-1).

SImilarly for ‘Merge’ function

$T(n)=2T(\frac{n}{2})+1$ and T(2)=1

Which comes out to be (n-1).
0 0 votes

for n=2    4 calls

for n=3    7 calls

for n=6     16 calls

thus for “n” calls “3n-2” calls

0 0 votes

Total number of MERGESORT() calls = 2n-1

Total number of RECURSIVE MERGESORT() calls = 2n-2 (The very first call will not be a recursive call)

Total number of MERGE() calls = n-1

Position:
Show:

Related questions

1 1 vote
1 1 answer
2.1k
2.1k views
rahul sharma 5 asked Mar 9, 2018
2,118 views
Consider the modified merge sort where we divide array into 5 equal sub arrays instead if 2(as in standard merge sort).What is the time complexity if modified merge sort?...
1 1 vote
2 answers 2 answers
1.1k
1.1k views
sumit goyal 1 asked Aug 9, 2017
1,062 views
HOW NO. OF LEVELS IS LOG N + 1 CAN ANYONE HELP ME , how to solve this and get log n + 1
1 1 vote
1 1 answer
136
136 views
GO Classes asked Aug 10
136 views
Mergesort recursively sorts the two halves of an array.After both recursive calls have finished, but before the merge operation, which statement must be true?The complete...
0 0 votes
0 0 answers
989
989 views
Nandkishor3939 asked Jan 21, 2019
989 views
What is the extra memory needed for merge sort:1] In case of Iterative merge sort.(DS:Array)2]In case of Recursive merge sort.(DS:Array)3] In case of Iterative merge sort...