Doubt in merge sort?

explain the time complexity of the merge sort?

@mehulbhandari358
for mergesort, this is the formula for time complexity with array size of N
T(N) = 2T(N/2) + O(N)
because we call mergesort for both halves(costing 2
T(N/2) ) and then merge them (costing O(N) )
now when we expand this formula,
we get T(N) = O(N) + 2O(N/2) + 4O(N/4) + … upto logN terms
hence T(N) = O(N)xlogN = O(NlogN).

what is T AND O here are they diiferent ?

T is the complexity of a function(merge sort in this case) and O is complexity of any operation.
T(N) = O(NlogN) means that a function with input size N takes time complexity of NlogN.

then how merge sort takes o(n) complexity ?

merge sort has a complexity of NlogN only!

no i am asking for merging the two arrays how does it takes o(n) time ?

@mehulbhandari358
we do this in mergesort
mergesort(a,s,mid)
mergesort(a,mid+1,e)
// now here the array from s to mid and mid+1 to e is already sorted!
hence now merging two sorted arrays takes linear time!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.