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

Discussion

What is the worst-case time complexity of insertion sort ?

  • A.O(n)
  • B.O(n log n)
  • C.\(O(n^{2})\)
  • D.O(log n)

Answer: C

The worst-case time of insertion sort is \(O(n^{2})\).

In the worst case, a reverse-sorted array, the i-th element has to be shifted past all i - 1 elements before it:

\[ 1 + 2 + \cdots + (n-1) = \frac{n(n-1)}{2} \]

That number of comparisons and shifts grows as n squared.

O(n) is insertion sort's best case, on input that is already sorted. O(n log n) is the bound for merge sort or heapsort, and O(log n) is a single binary search, not a full sort.

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

Post your comment

Please log in or register to post a comment.