The Gateway to Computer Science Excellence

First time here? Checkout the FAQ!

x

+29 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

- $O (n \log n) $
- $ O(n^{2} \log n) $
- $ O(n^{2} + \log n) $
- $ O(n^{2}) $

+1

Hello @Arjun sir, the answer for the above is O(n^2logn) can you pls explain how to solve this question ?

+14

[1] In merge sort of array of length "n" you get a tree of height log(n).

[2] If single element of array is just an integer then at any level of tree, in worst case there will be n comparisons in merge process.

[3] Now if the single element of array is itself an array of size "m" then in order to find larger of 2 elements you need to compare all the m elements.

[4] So at any any level there are n comparisons and each comparison costs "m", so total nmlog(n)

[2] If single element of array is just an integer then at any level of tree, in worst case there will be n comparisons in merge process.

[3] Now if the single element of array is itself an array of size "m" then in order to find larger of 2 elements you need to compare all the m elements.

[4] So at any any level there are n comparisons and each comparison costs "m", so total nmlog(n)

+51 votes

Best answer

+1

right approach i guess...

want to put in other words like:-

lets take as string has length N (change n to N for some time)

so first of all we will sort the list of n strings for the first characters of all strings (it will take n log n time).

[something like radix sort] so for second time we will sort for 2nd characters (which also take n log n time) and then for 3rd char. then for 4th and so on till N characters.

In this way it will take N*n lg n or n*n lg n or **n ^{2 }lg n.**

plz correct me wherever i m wrong.

0

what if we assign a weight for every string which can be done in O(N^2) time and do merge sort on 'n' weights which will be O(N^2 + NlogN ) = O(N^2) ---> then option will be D ... correct if i'm wrong.. ?

0

How will this approach work?If we have sorted the first character of each string,now what is next step? We will read the next character from each string ?Then how will this be merged with previous sequence?

0

Since our array is of characters. We need to compare with all the subsequent elements before the placement.

There are logn levels since it is merge sort. For n number of characters we perform n comparisons, so O(n^{2}) time required and it is done in logn levels. So complexity is O(n^{2} * logn).

0

@Shaik Masthan @Ayush Upadhyaya is this correct way to approach this problem.

1. There are n list each one having n elements. SO total elements are n^2.

2.Now to make list of size n/2, we will have to compare and move all n^2 elements from one level to other level (merge algo in merge sort).

3. We will have to do this till we get single list. Now when we will get one list? when n/2^k =1 i.e k=logn levels.

So time complexity is work done * number of levels = n^2 * logn.

0

suppose there are n strings. Now moving one level up in lexicographic order. To compare two strings we need O(n) time. Hence for total of n list it will be n* n/2 = O(n^2) right ?

0

@tusharp-Yes you are correct tushar and your earlier analysis also seems correct.I am sorry I misinterpreted it.

Let us say we have two strings char *a[] ,char *b[] and we want to merge in one list char *c[]

i,j=0;

while(a[i++]==b[j++]); //Almost O(n) comparisons in worst case

if(a[i-1]<b[j-1])//a is lexicographically smaller than b

{ i=0;

while(a[i]!='\0')

c[k++]=a[i++];

j=0;

while(b[j]!='\0')

c[k++]=b[j++];

c[k]='\0';

//a is copied before c in merged string.

And O(n) swaps for each two strings of length n.

}

So, from last level to last-1 level, total comparisons/swaps=$n \times \frac{n}{2}=O(n^2)$

This will be same story for each level.

How many levels?

Untill $\frac{n}{2^l}=1$

$l=log_2n$

Hence total TC-$O(n^2log_2n)$

+16 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)
```

+11 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

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

+5 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(n^{2}).

now imagine that merge sort binary tree.. in that ,at every level O(n^{2}) work is done , and there are Logn such levels therefore total time taken wil be **O(n ^{2}logn).**

+5 votes

The function Compare2strings will take input two strings and output the lexicographic order in which they should appear

compare2strings(String s1, String s2)

{

int ptr=0;

while( s1[ptr] = = s2[ptr] && ptr < n )

{ ptr++;

}

if( s1[ptr] < = s2[ptr]

return (s1,s2)

else if( s1[ptr] > s2[ptr]

return (s2,s1)

} // This function takes worst case O(n) to compare two strings.......

MergeSort(Sequence 1.......nth strings)

{

if(Single string is given as input)

return string

else

{

MergeSort(Sequence 1,2,3.....n/2th strings)

MergeSort(Sequence (n/2+1),(n/2+2).......nth strings)

Merge(Sequence 1,2,3.....n/2th strings, Sequence (n/2+1),(n/2+2).......nth strings )

}

}

Merge(Seq1 , Seq2)

{ Worst case number of string comparisons among strings = O(n + n-1) = O(n)

with each comparison O(n) using compare2strings function

Merge cost in worst case = O(n).O(n) = O(n^2)

}

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

Master theorem T(n) = O(n^2)

so B, C, D all are answers. Maybe that is the reason marks are awarded to all

compare2strings(String s1, String s2)

{

int ptr=0;

while( s1[ptr] = = s2[ptr] && ptr < n )

{ ptr++;

}

if( s1[ptr] < = s2[ptr]

return (s1,s2)

else if( s1[ptr] > s2[ptr]

return (s2,s1)

} // This function takes worst case O(n) to compare two strings.......

MergeSort(Sequence 1.......nth strings)

{

if(Single string is given as input)

return string

else

{

MergeSort(Sequence 1,2,3.....n/2th strings)

MergeSort(Sequence (n/2+1),(n/2+2).......nth strings)

Merge(Sequence 1,2,3.....n/2th strings, Sequence (n/2+1),(n/2+2).......nth strings )

}

}

Merge(Seq1 , Seq2)

{ Worst case number of string comparisons among strings = O(n + n-1) = O(n)

with each comparison O(n) using compare2strings function

Merge cost in worst case = O(n).O(n) = O(n^2)

}

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

Master theorem T(n) = O(n^2)

so B, C, D all are answers. Maybe that is the reason marks are awarded to all

- All categories
- General Aptitude 1.6k
- Engineering Mathematics 7.5k
- Digital Logic 3k
- Programming & DS 4.9k
- Algorithms 4.3k
- Theory of Computation 6k
- Compiler Design 2.1k
- Databases 4.2k
- CO & Architecture 3.5k
- Computer Networks 4.2k
- Non GATE 1.4k
- Others 1.5k
- Admissions 584
- Exam Queries 571
- Tier 1 Placement Questions 23
- Job Queries 72
- Projects 18

50,115 questions

53,224 answers

184,676 comments

70,474 users