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

Discussion

Which sorting algorithm has worst-case time complexity of O(n2) ?

  • A.Merge Sort
  • B.Quick Sort
  • C.Heap Sort
  • D.Counting Sort

Answer: B

The algorithm with worst-case time O(n2) is Quick Sort.

Quick sort is O(n log n) on average, but if the pivot chosen is always the smallest or largest element (for example, taking the first element of an already sorted array), each partition removes only one element and the work grows as n + (n - 1) + ... + 1, which is O(n2).

Merge sort and heap sort are O(n log n) even in the worst case. Counting sort is not a comparison sort and runs in O(n + k), where k is the range of the keys.

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

Post your comment

Please log in or register to post a comment.