Skip to content
Official PYQ · 2012

GATE CS 2012 Q39 — Searching, Sorting, Hashing

Explain with AI

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

Choose one option
View worked solution

Answer: B

To find the worst-case running time of sorting nn strings, each of length nn, using merge-sort, we analyze the two main operations performed during the algorithm: string comparisons and string copying.

1. Comparison Cost:

  • Standard merge-sort on nn elements requires O(nlog⁡n)O(n \log n) comparisons in the worst case.
  • Comparing two strings of length nn lexicographically takes O(n)O(n) time in the worst case (since we may need to compare up to all nn characters).
  • Therefore, the total time spent on comparisons is:
    Comparison Time=O(nlog⁡n)×O(n)=O(n2log⁡n)\text{Comparison Time} = O(n \log n) \times O(n) = O(n^2 \log n)

2. Copying Cost:

  • At each of the log⁡n\log n levels of the merge-sort recursion tree, all nn strings are copied into auxiliary arrays during the merge step.
  • Copying a single string of length nn takes O(n)O(n) time.
  • Thus, the copying cost at each level is O(n×n)=O(n2)O(n \times n) = O(n^2).
  • Across all log⁡n\log n levels, the total copying time is:
    Copying Time=O(n2)×log⁡n=O(n2log⁡n)\text{Copying Time} = O(n^2) \times \log n = O(n^2 \log n)

Combining both costs, the overall worst-case running time is O(n2log⁡n)O(n^2 \log n).

Hence, the correct option is (B).

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Searching, Sorting, Hashing questions