Introduction to Algorithms
Lecture 3

2
Recap
•
Master Theorem
•
Unbalanced recursion trees
•
Divide-and-conquer paradigm
•
Strassen’s algorithm
•
Polynomial multiplication

3
Today
•
Heaps and Heapsort
•
Priority Queues
•
Quicksort
•
Probability
•
Randomized Algorithms

4
Heapsort
•
Properties:
–
O(
n
lg
n
)
– like merge sort, but unlike insertion
–
Sorts in place – like insertion, but unlike merge
–
Heapsort combines better attributes of these
sorting algorithms

5
Heapsort
•
Data Structure:
binary
heap
A
–
Simple array
–
Viewed as a nearly complete binary tree
–
heap property
:
A
[parent(
x
)]
A
[
x
]
This defines a
max-heap
•
There are also min-heaps and
k
-ary heaps

6
Heaps: Example
•
Notice the implicit tree links: Children of node
i
are
2
i
and
2
i
+1
•
Why is this useful?
Multiplication by
2
is a left shift in binary or add
to self -
fast
16
14
10
8
7
2
4
1
9
3
1
2
3
4
5
6
7
8
9
10

7
Heaps: Extract-Max
Heap-Extract-Max(
A
)
1.
//Removes and returns largest element of
A
2.
max
A
[1]
3.
A
[1]
A
[
n
]
4.
n
n
1
5.
Heapify(
A
,1
)
//Remakes heap
6.
return
max
Running time?
(1)
+ Heapify time.

8
Heaps: Heapify
Heapify(
A
,
i
):
•
i
is index into array
A
•
Binary trees rooted at
Left(
i
)
and
Right(
i
)
are
heaps
•
But,
A
[
i
]
may be smaller than its children, thus
violating the heap property.
•
Heapify makes
A
a heap once more.
•
How?
Move
A
[
i
]
down in heap until heap property is
satisfied.

9
Heaps:
Heapify
Heapify(
A
,
i
)
//
n
is total number of elements
1.
//Left & Right subtrees of
i
are heaps
2.
//Makes subtree rooted at
i
a heap
3.
l
Left(
i
)
//
l
=2
i
4.
r
Right(
i
)
//
r
=2
i
+1
5.
if
l
n
and
A
[
l
]
A
[
i
]
6.
then
largest
l
7.
else
largest
i
8.
if
r
n
and
A
[
r
]
A
[
largest
]
9.
then
largest
r
10.
if
largest
i
11.
then exchange
A
[
i
]
A
[
largest
]
12.
Heapify(
A
,
largest
)

10
Heapsort – comments
•
Correctness
: induction on the height of
i
•
Analysis
: time proportional to height of
i
=
O
(lg
n
)
•
For faster code, use a loop instead of
recursion.

11
Heaps: Heapify Example
1.
Call Heapify(
A
, 2
)
16
4
10
14
7
2
8
1
9
3
1
2
3
4
5
6
7
8
9
10
i

12
Heaps: Heapify Example (cont.)
2.
Exchange
A
[2]
with
A
[4]
and recursively
call Heapify(
A
, 4
)
16
14
10
4
7
2
8
1
9
3
1
2
3
4
5
6
7
8
9
10
i

13
Heaps: Heapify Example (cont.)
3.
Exchange
A
[4]
with
A
[9]
and recursively
call Heapify(
A
, 9
)
16
14
10
8
7
2
4
1
9
3
1
2
3
4
5
6
7
8
9
10
i

14
Heaps: Heapify Example (cont.)
4.
Node
9
has no children, so we are done.
16
14
10
8
7
2
4
1
9
3
1
2
3
4
5
6
7
8
9
10

15
Heaps: Heapsort
Heapsort(
A
)
Analysis
1.
Build-Heap(
A
)
??
2.
for
i
n
downto
2
n
times
3.
do exchange
A
[1]
A
[
i
]
O
(1)
4.
n
n
–1
O
(1)
5.
Heapify(
A
,1
)
O
(lg
n
)
Total Running time
:
O
(
n
lg
n
)
+ Build-Heap(
A
) time

16
Heaps: Building a heap
•
Convert an array
A
[1..
n
]
, where
n
=
length[
A
]
,
into a heap.
•
Notice that the elements in the subarray
A
[(
n
/2+1)..
n
]
are already
1
-element heaps
to begin with.

#### You've reached the end of your free preview.

Want to read all 62 pages?

- Fall '05
- RudolfFleischer