Machine-translated by Google. Questions are authored in English.
Top

Discussion

The worst case running times of Insertion sort, Merge sort and Quick sort, respectively, are:

  • A.\(\Theta(n\log n)\), \(\Theta(n\log n)\), and \(\Theta(n^{2})\)
  • B.\(\Theta(n^{2})\), \(\Theta(n^{2})\), and \(\Theta(n\log n)\)
  • C.\(\Theta(n^{2})\), \(\Theta(n\log n)\), and \(\Theta(n\log n)\)
  • D.\(\Theta(n^{2})\), \(\Theta(n\log n)\), and \(\Theta(n^{2})\)

Answer: D

The worst case times are \(\Theta(n^{2})\), \(\Theta(n\log n)\), and \(\Theta(n^{2})\).

Insertion sort: on reverse-sorted input each element shifts past all the earlier ones, \(1 + 2 + \cdots + (n-1) = \Theta(n^{2})\). Merge sort: it always halves the array and merges in linear time, \(\Theta(n\log n)\) in every case. Quick sort: if the pivot is always the smallest or largest element (for example the first element of sorted input), each partition removes only one element, giving \(\Theta(n^{2})\).

Quick sort's \(\Theta(n\log n)\) is its average case, which is the trap in the other options; insertion sort's worst case is never \(n\log n\).

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.