in Algorithms
1,510 views
0 votes
0 votes

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

(A) O (n log n (B) O (n^2 log n) (C) O (n^2 + log n) (D) O (n^2)

in Algorithms
1.5k views

3 Answers

1 vote
1 vote

you know that for sorting n elements it take n.log(n) time

why this is happened ?

due to element comparission take constant time. ( at each level )

 

but here element comparission takes O(n) times ====> time complexity of required question = O(n2 log n)

2 Comments

Sir, please explain why comparison in lexicographical order is taken O(n) times rather than O(1).
0
0

if it is integer, comparssions take O(1) ---------- due to a < b or b < a or a=b

if it is string with length 'n' , then comparission takes O(n) ------- due to you have to compare each letter in the strings in the worst case

ex:- "ABC", "ABD", how these two strings compared ( string length=3, in worst case it requires 3 comparissions )?

A == A, Proceed Further

B == B, Proceed Further

C ==D, not equal ===> as per lexograhical order, C < D ===> "ABC" < "ABD"

2
2
0 votes
0 votes
In lexicographical order you don't need to sort a string. You have to compare strings and sort lists as dictionary. To compare 2 strings(size=n) It will take O(n).

To sort n lists merge sort take O(nlogn).

So finally n*nlogn = O(n^2logn).
0 votes
0 votes

n = length of string

i = n

Step X :pick the i’th character of each string and sort all the string on the basis of the i’th character

this took O(nlogn) time

i = i – 1

repeat step X

Doing this for n times makes the complexity = O(n^2 logn)

 

Reason this worked: Merge sort is a stable sort

 

Ans: B