Heap sort analysis
In the analysis of heap sort we know that one of the ways to perform heap sort is in two phases.
- Create a heap i.e., max or min priority queue.
- Remove the maximum/minimum from priority queue until all elements have been removed
Suppose there are N
elements. For phase 1 the number of comparisons is 2N
and for one removal the number of comparisons is 2lgN
so for N removals it is 2NlgN
comparisons. So total number of comparisons is 2N + 2NlgN
.
This is my understanding. The book I am reading is Algorithms analysis by Wesis which says:开发者_如何转开发
In the second phase, the i
th deletemax
uses at most 2(floor(log i))
comparisons, for a total of at most 2NlogN - O(N)
comparsions (assuming N >= 2
). Consequently in the worst case, there are at most 2NlogN - O(n)
comparisons for heap sort.
My questions are
How did the author conclude that in the second phase, the
i
thdeletemax
uses at most2(floor(logi))
comparisons?How did the author come up with a total of
2NlogN - O(N)
?
精彩评论