edited by
28,614 views
71 votes
71 votes

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

  1. $O (n \log n) $
  2. $ O(n^{2} \log n) $
  3. $ O(n^{2} + \log n) $
  4. $ O(n^{2}) $
edited by

14 Answers

Best answer
104 votes
104 votes
We are given the first character of each $n$ strings. To sort them, it will take $O(n \log n)$ time. In the worst case we may have to do the above process $2$ times, $3$ times, $\ldots ,n$ times. So, $n*O(n \log n)=O(n^2 \log n).$

Correct Answer: $B$
edited by
22 votes
22 votes
**answer is O(n^2logn)
  , 
we know Merge sort has recurrence form
T(n) = a T(n/b) + O(n)
in case of merge sort 
it is 
T(n) = 2T(n/2) + O(n) when there are n elements
but here the size of the total is not "n" but "n string of length n"
so a/c to this in every recursion we are breaking the n*n elements in to half
for each recursion as specified by the merge sort algorithm
MERGE-SORT(A,P,R)  ///here A is the array P=1st index=1, R=last index in our case it 
                      is n^2 
if P<R
then Q = lower_ceiling_fun[(P+R)/2]
      MERGE-SORT(A,P,Q)
      MERGE-SORT(A,Q+1,R)
      MERGE (A,P,Q,R)
MERGE(A,P,Q,R) PROCEDURE ON AN N ELEMENT SUBARRAY TAKES TIME O(N)
BUT IN OUR CASE IT IS N*N
SO A/C to this merge sort recurrence equation for this problem becomes
T(N^2)= 2T[(N^2)/2] + O(N^2)
WE CAN PUT K=N^2 ie.. substitute to solve the recurrence
T(K)= 2T(K/2) + O(K)
a/c to master method condition T(N)=A T(N/B) + O(N^d)
                               IF A<=B^d  then T(N)= O(NlogN)
therefore T(K) = O(KlogK)
substituting K=N^2
we get T(N^2)= O(n*nlogn*n)
       ie.. O(2n*nlogn)
         .. O(n*nlogn)

 

22 votes
22 votes
The worst case complexity of merge sort is O(n log n). Hence the worst case complexity for sorting first letter of n strings will be O(nlog n).

In worst case, we have to sort all n letters in each string.

Hence total complexity will be upto O(n * n log n) = O( n^2 log n)

Answer is B
1 flag:
✌ Edit necessary (Ray Tomlinson “needed to explain properly instead of just drawing conclusion”)
10 votes
10 votes

Lets take a string of length n , we need n comparisons to sort it , and there are n such strings .

now string of length n requires n comparisons , if we have  n such strings , Total time for comparisons will be O(n2).

now imagine that merge sort binary tree.. in that ,at every level O(n2) work is done , and there are Logn such levels therefore total time taken wil be O(n2logn).

Answer:

Related questions

59 votes
59 votes
5 answers
2
42 votes
42 votes
4 answers
3
35 votes
35 votes
3 answers
4
gatecse asked Aug 5, 2014
15,482 views
The recurrence relation capturing the optimal execution time of the $Towers \ of \ Hanoi$ problem with $n$ discs is$T(n) = 2T(n − 2) + 2$$T (n) = 2T(n − 1) + n$$T (...