简体   繁体   English

如何证明quicksort的时间复杂度是O(nlogn)

[英]How to prove the time complexity of quicksort is O(nlogn)

I don't understand the proof given in my textbook that the time complexity of Quicksort is O(n log n).我不明白我的教科书中给出的快速排序的时间复杂度是 O(n log n) 的证明。 Can anyone explain how to prove it?谁能解释一下如何证明?

Typical arguments that Quicksort's average case running time is O(n log n) involve arguing that in the average case each partition operation divides the input into two equally sized partitions. Quicksort 的平均情况运行时间为 O(n log n) 的典型论点涉及到在平均情况下每个分区操作将输入划分为两个相等大小的分区。 The partition operations take O(n) time.分区操作需要 O(n) 时间。 Thus each "level" of the recursive Quicksort implementation has O(n) time complexity (across all the partitions) and the number of levels is however many times you can iteratively divide n by 2, which is O(log n).因此,递归快速排序实现的每个“级别”的时间复杂度为 O(n)(跨所有分区),并且级别数是很多次您可以迭代地将 n 除以 2,即 O(log n)。

You can make the above argument rigorous in various ways depending on how rigorous you want it and the background and mathematical maturity etc, of your audience.您可以根据您想要的严谨程度以及您的听众的背景和数学成熟度等,以各种方式使上述论证变得严谨。 A typical way to formalize the above is to represent the the number of comparisons required by the average case of a Quicksort call as a recurrence relation like将上述形式化的典型方法是将 Quicksort 调用的平均情况所需的比较次数表示为递归关系,例如

T(n) = O(n) + 2 * T(n/2)

which can be proved to be O(n log n) via the Master Theorem or other means.可以通过主定理或其他方式证明为 O(n log n)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM