我知道这个复杂度是O(nlog(n))。但是为什么呢?你是怎么得出这个答案的?
非常感谢您的帮助,我非常想知道!
非常感谢您的帮助,我非常想知道!
平均情况下,它的复杂度被认为是 O(n log(n))
,而在最坏情况下,它需要 O(n^2)
(二次方)。
考虑以下伪代码:
QuickHull (S, l, r)
if S={ } then return ()
else if S={l, r} then return (l, r) // a single convex hull edge
else
z = index of a point that is furthest (max distance) from xy.
Let A be the set containing points strictly right of (x, z)
Let B be the set containing points strictly right of (z, y)
return {QuickHull (A, x, z) U (z) U QuickHull (B, z, y)}
r
和最左上角的点l
。查找这些极值需要O(n)
的时间。n
步来确定极值点z
,但是递归调用的成本取决于集合A
和集合B
的大小。T(n) = 2 T(n/2) + O(n)
。T(n) = O(n log(n))
。T(n) = T(n-1) + O(n)
= T(n-1) + cn