All Exams Test series for 1 year @ ₹349 only
Question

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

The correct answer is

O(n 2log n)

Understanding Merge Sort and String Sorting Complexity

The question asks about the worst-case running time of the merge sort algorithm when sorting a list of \(n\) strings, where each string has a length of \(n\). Let's break down the components that contribute to the time complexity in this specific scenario.

Merge Sort Algorithm Overview

Merge sort is a divide-and-conquer sorting algorithm. It works by:

  • Recursively dividing the list into sublists until each sublist has only one element (which is considered sorted).
  • Repeatedly merging sublists to produce new sorted sublists until one sorted list remains.

For a list of \(n\) elements, the merge sort algorithm performs approximately \(O(n \log n)\) comparisons and requires \(O(n \log n)\) work for moving elements during the merging process, assuming element operations (like comparison) take constant time.

Complexity When Sorting Strings

When sorting simple data types like integers or numbers, the comparison operation (\(<,\ >,\ =\)) takes constant time, i.e., \(O(1)\). However, when sorting strings, comparing two strings involves comparing their characters one by one until a difference is found or one string ends.

If we are comparing two strings of length \(m\), the comparison operation in the worst case (when strings are identical or differ only at the last character) takes \(O(m)\) time. In this problem, each string has a length of \(n\). Therefore, comparing two strings takes \(O(n)\) time in the worst case.

Calculating Worst Case Running Time

Let's analyze the impact of string comparison time on the overall merge sort complexity.

  1. Number of Comparisons: The structure of merge sort dictates that it performs \(O(n \log n)\) comparisons to sort \(n\) items. This part of the complexity remains the same regardless of what is being sorted.
  2. Cost per Comparison: Each comparison involves comparing two strings of length \(n\). As discussed, this takes \(O(n)\) time in the worst case.
  3. Total time for Comparisons: Multiplying the number of comparisons by the cost per comparison, we get \(O(n \log n) \times O(n) = O(n^2 \log n)\).
  4. Cost of Merging: The merging step in merge sort involves copying elements (strings in this case) from temporary storage back to the original list. In a standard merge operation of two sorted sublists totaling \(k\) elements, \(O(k)\) elements are copied. When dealing with strings, copying a string of length \(n\) takes \(O(n)\) time. Across all levels of the merge sort, the total number of string copy operations remains \(O(n \log n)\) in terms of element movements, but the total amount of data moved is \(O(n)\) total elements across different levels for \(O(\log n)\) passes. A simpler way to think about merging cost is that at each level of the merge sort tree, a total of \(n\) elements (strings) are processed for merging. Moving each string of length \(n\) takes \(O(n)\) time. Thus, processing one level of merging takes \(O(n \times n) = O(n^2)\) time. Since there are \(O(\log n)\) levels in merge sort, the total time for merging operations is \(O(n^2 \log n)\).

The total time complexity is determined by the sum of the time spent on comparisons and the time spent on merging (copying strings). Both turn out to be \(O(n^2 \log n)\). Therefore, the dominant term is \(O(n^2 \log n)\).

Evaluating the Options

Let's look at the given options:

Option Complexity Analysis
1 \(O(n \log n)\) This is the complexity for sorting \(n\) items with \(O(1)\) comparison time. Incorrect because string comparison is \(O(n)\).
2 \(O(n^2 \log n)\) This matches our calculated worst-case time complexity for sorting \(n\) strings of length \(n\).
3 \(O(n^2 + \log n)\) This complexity does not align with the merge sort structure and string comparison cost.
4 \(O(n^3)\) This might be the complexity for algorithms like bubble sort or insertion sort on strings of length \(n\), where each comparison is \(O(n)\) and there are \(O(n^2)\) comparisons. Merge sort is more efficient in terms of the number of comparisons.

Based on the analysis, the worst-case running time for sorting \(n\) strings, each of length \(n\), using merge sort is \(O(n^2 \log n)\).

Revision Table: Key Concepts

Concept Description Complexity/Note
Merge Sort Comparisons Number of element comparisons in merge sort for \(n\) items. \(O(n \log n)\)
String Comparison Comparing two strings of length \(L\). \(O(L)\) in worst case
String Comparison (Length n) Comparing two strings of length \(n\). \(O(n)\) in worst case
Total Comparison Cost (Strings) (Number of comparisons) \(\times\) (Cost per comparison) \(O(n \log n) \times O(n) = O(n^2 \log n)\)
Merge Cost (Strings) Cost of copying strings during the merge phases over all levels. \(O(n^2 \log n)\)
Overall Merge Sort (Strings) Sum of comparison and merge costs. \(O(n^2 \log n)\)

Additional Information: String Sorting Algorithms

While comparison-based sorts like merge sort, quicksort, and heapsort can be used for strings by adapting the comparison operation, their time complexity depends on the string length. For a list of \(N\) strings, each of maximum length \(L\), the complexity of these algorithms becomes \(O(N \log N \times L)\) in the worst case.

Other sorting algorithms specifically designed for strings, known as radix sorts, can sometimes be faster depending on the total length of all strings and the alphabet size. Examples include:

  • Least Significant Digit (LSD) Radix Sort: Sorts strings character by character from right to left. For \(N\) strings of length \(L\), it can take \(O(N \times L)\) time using counting sort as the stable sorting sub-routine for each character position (assuming a fixed alphabet size).
  • Most Significant Digit (MSD) Radix Sort: Sorts strings character by character from left to right, using recursion for sub-buckets. Its performance depends on common prefixes and the distribution of characters. In the worst case, it can also approach \(O(N \times L)\) if there are many similar prefixes, but its average case can be better in some scenarios.

In this problem, \(N=n\) and \(L=n\), so the comparison-based merge sort has a worst-case complexity of \(O(n \log n \times n) = O(n^2 \log n)\).

Was this answer helpful?

Important Questions from Sorting - Teaching

  1. What is the best case time complexity of radix sort algorithm?

  2. Merge sort uses:

  3. A sorting technique that guarantees that records with the same primary key occurs in the same

    order in the sorted list as in the original unsorted list is said to be
  4. Which of the following algorithm design approach is used in Quick sort algorithm?

  5. Which of the following is best running time to sort n integers in the range 0 to n 2-1

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App