What is the worst case scenario for quicksort?

后端 未结 6 844
醉酒成梦
醉酒成梦 2020-12-03 11:28

When does the quicksort algorithm take O(n^2) time?

6条回答
  •  有刺的猬
    2020-12-03 11:34

    The factors that contribute to the worst-case scenario of quicksort are as follows:

    • Worst case occurs when the subarrays are completely unbalanced
    • The worst case occurs when there are 0 elements in one subarray and n-1 elements in the other.

    In other words, the worst-case running time of quicksort occurs when Quicksort takes in a sorted array (in decreasing order), to be on the time complexity of O(n^2).

提交回复
热议问题