*This preview shows
pages
1–3. Sign up to
view the full content.*

1
Sorting using heaps
We can first build heap, then
repeat: remove max
.
Lecture 8, Oct 14 2010
In place:
BuildHeap
for i=n down to 2
exch A(1), A(i)
Heapify(A,1,i-1)
Essentially same as the first approach.
93
We use the fact that:
»
heap becomes smaller after “remove max”,
»
last array entry becomes free.
Example
Sort:
8
5
7
3
2
1
?
1
5
7
3
2
8
?
7
5
1
3
2
8
?
2
5
1
exchange
exchange
heapify
5
3
1
heapify
94
3
7
8
?
2
7
8
?

This ** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*2
Lower bound for sorting
All sorting algs that we saw :
comparison-sorts
only operation allowed on data is comparison.
Is
O(n log n)
the best we can do in this case ?
Represent computation by
decision tree
:
95
Here 1:2 means compare first with second input; <3,1,2> means
that third element was the minimum, 2
nd
was the maximum.
Execution - walk from root to a leaf; longest walk – worst case time
More lower bound
1 leaf per each possible answer.
n!
different answers
at least
n!
leaves.

This is the end of the preview. Sign up
to
access the rest of the document.