3

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.

0

3 Answers 3

3

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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.

5 Comments

What do you mean by each string is O(nlgn)?
@meowgoesthedog Do dogs really meow?
Is the answer to my question really that obvious? I don't think the OP means to sort the strings themselves internally
My understanding of the question is that we have n strings, each of whose characters must be sorted. Since each string has n characters, then sorting via mergesort would be an O(nlgn) operation, O(n^2*lgn) for all n strings. But there is a second part to the question, namely sorting the n words. This again is an O(nlgn) operation; but it doesn't change the overall running time. But maybe I'm wrong here.
I think OP means to sort the strings as they would be in a dictionary (hence lexicographic order)
0

arunk2's answer is correct, but here are some more details of exactly where the recurrence is going wrong:

In normal merge sort of numbers, the recurrence is: T(n) = 2T(n/2) + cn ; where c is some constant

Notice what happens while solving this recurrence, and the difference while solving the recurrence T'(n) with c(n^2)

T(n) = 2T(n/2) + cn

T(n) = 2[2T(n/(2^2)) + c(n/2)] + cn

T(n) = (2^2)T(n/(2^2)) + cn + cn //after logn steps, 'cn' will be added logn times, therefore resulting in O(nlogn)


T'(n) = 2T(n/2) + c(n^2)

T'(n) = 2[2T(n/(2^2)) + c((n/2)^2)] + c(n^2)

T'(n) = (2^2)T(n/(2^2)) + c(n^2)/2 + c(n^2) //every time n^2 is halving, which results in O(n^2)

If you analyze the algorithm closely, (and as mentioned in arun's answer), the work done at each level is always n^2, and it doesn't halve at each level, and that's why the time complexity is O((n^2)logn) instead of simply O(n^2) by masters theorem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.