How to calculate order (big O) for more complex algorithms (eg quicksort)

后端 未结 6 624
春和景丽
春和景丽 2021-01-30 02:54

I know there are quite a bunch of questions about big O notation, I have already checked:

  • Plain english explanation of Big O
  • Big O, how do you calculate/a
6条回答
  •  礼貌的吻别
    2021-01-30 03:15

    For some algorithms, getting a tight bound for the running time through intuition is close to impossible (I don't think I'll ever be able to intuit a O(n log log n) running time, for instance, and I doubt anyone will ever expect you to). If you can get your hands on the CLRS Introduction to Algorithms text, you'll find a pretty thorough treatment of asymptotic notation which is appropriately rigorous without being completely opaque.

    If the algorithm is recursive, one simple way to derive a bound is to write out a recurrence and then set out to solve it, either iteratively or using the Master Theorem or some other way. For instance, if you're not looking to be super rigorous about it, the easiest way to get QuickSort's running time is through the Master Theorem -- QuickSort entails partitioning the array into two relatively equal subarrays (it should be fairly intuitive to see that this is O(n)), and then calling QuickSort recursively on those two subarrays. Then if we let T(n) denote the running time, we have T(n) = 2T(n/2) + O(n), which by the Master Method is O(n log n).

提交回复
热议问题