Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting n strings using Merge sort

A list of n strings each of length n is sorted into lexicographic order using the merge sort algorithm. The worst case running time of this computation is?

I searched this question and everywhere I found the answer to be: O(n2logn).

While my approach is as follows:
Comparing two strings require O(n) comparisons, hence each merge will take O(n2) time.
So, the recurrence equation will be:
T(n) = 2T(n/2) + O(n2)
Therefore, by Master Method:
T(n) = O(n2).

Correct me where I am wrong.

like image 325
Rishabh Gupta Avatar asked Dec 19 '25 11:12

Rishabh Gupta


2 Answers

Your analysis and reduction is correct. But the problem is in the interpretation of recurrence equation of master theorem.

T(n) = 2T(n/2) + O(n2)

The n in the equation represents no.of elements in the list to be sorted. The O(n2) is not entirely correct. It is O(n * n-character comparisons). This will be evident if we substitute 'n-character comparisons' operations with a different complexity, which is independent of no.of elements. Let it be O(m). Then we have

T(n) = 2T(n/2) + O(n * m)

we have a = 2, b = 2, c = 1 and c = Logba (case 2)

Hence, T(n) = O(n * m * log n)

Now, substituting m with n

T(n) = O(n2 * log n)

We can also justify this as - the recurrence tree for merge sort will have height Log(n). O(n2) work will be done at each level of the recurrence tree in merge operation.

Hence, a total of O (n2 log n)

Hope it helps!

like image 134
arunk2 Avatar answered Dec 21 '25 02:12

arunk2


I think that your conclusion of O(n^2*lgn) is correct. If you were dealing with a set of n numbers, we know that mergesort would require O(nlgn). The difference here is that we are now dealing with strings, each of which is n long. The only impact on the mergesort algorithm would be the comparison of two strings in the base case. Instead of an O(1) comparison of two numbers, we would instead have to compare the two strings. In the general case, this is an O(n) operation, because we might have to walk down each of the two length n strings.

like image 37
Tim Biegeleisen Avatar answered Dec 21 '25 02:12

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!