edited by
29,230 views
72 votes
72 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

0 votes
0 votes

We have n words(strings) each of length n. Since both of them are n let's take the length of the word as k(=n). So we have n words each of length k. Comparing 2 words of length k in the worse case will be k comparisons. In the next level, we have words of length 2k each consisting of two words. To compare 2k and 2k words at worse we can have k+k+k =3k(2k+2k-k) comparisons. In the next level we have 7k(4k+4k-k) comparisons, in the next level we have 15k comparisons and so on. The depth of the recursion will be logn.

​​​​​​​

0 votes
0 votes

We have to sort Lexicographically.
First we will sort based on 1st character of each string.
This will take O(nlogn) time as there are n Strings in total (thus ‘n’ number of 1st characters)

In the Worst Case  i.e. the situation like abbcd, abbcf, abbcb , abbce, abbca

Here we have 5 strings of 5 characters each.

Each string has all first 4 characters same, therefore we have to sort based on last digit here

Now O(nlogn) was for 1st character of strings, Similarly we will sort 2, 3, 4, …. n times (here n is for sorting based on last digit)

So, n * O(nlogn) = O(n^2 logn)

Option B is correct.

Answer:

Related questions

6 votes
6 votes
5 answers
13
admin asked Mar 31, 2020
1,649 views
Merge sort uses :Divide-and-conquerBacktrackingHeuristic approachGreedy approach
48 votes
48 votes
2 answers
14
Kathleen asked Sep 23, 2014
22,985 views
If one uses straight two-way merge sort algorithm to sort the following elements in ascending order: $20, \ 47, \ 15, \ 8, \ 9, \ 4, \ 40, \ 30, \ 12, \ 17$then the o...