Lecture4
Building a heap¶
对应的算法:
A.heap-size = A.length
for i = A.length / 2 downto 1
MAX-HEAPIFY(A, i)
这实际上是一个线性的算法,我们下面来说明它是线性的。
考虑一个高为 \(\log n\) 的堆,考虑它的第 \(h\) 层
Property
An n-element heap has height \(\lfloor \log n \rfloor\) and at most \(\lceil n / 2^{h+1} \rceil\) nodes of any height \(h\).
There is a upper bound
Quick sort¶
Expected time analysis¶
Let \(T(n)\) be the random variable for the running time of RANDOMIZED-QUICKSORT
Define the indicator random variable
We have
Assume that \(E[T(n)] = O(n \log n)\)
Remark
maybe the following inequality also works
Lemma
Let \(X\) be # comparisons in Partition over entire execution of Quciksort on an \(n\)-element array. Then the running time of QuickSort is \(O(n + X)\)
We define the indicator
\(A\) means that \(z_i\) is compared to z_j
We have the partition of random variable
We have the following Expection
Then by the linearity of Expection
By the knowledge of harmonic series
Why the quicksort quick¶
it compare with a continue array in memory