# Heap And Heap Sort

Document Sample

```					Heap And Heap Sort

Ethan Coulter
4/22/2004
CS 146 – Dr. Lee
What is a heap?
A heap data structure is a data structure that
stores a collection of objects (with keys), and
has the following properties:

– Complete Binary tree
– Heap Order

It is implemented as an array where each
node in the tree corresponds to an element of
the array.
A Complete Binary Tree

A Complete Binary Tree is a binary tree that is
completely filled on all levels with a possible
exception where the lowest level is filled from
left to right.
Heap Order Property

For every node v, other than the root, the
key stored in v is greater or equal
(smaller or equal for max heap) than the
key stored in the parent of v.

In this case the maximum value is stored
in the root
Max Heap Example
Properties Of The Heap

The parent of node v, stored in A[i], is
stored in A[i/2]
• The left child of node v, stored in A[i], is stored in A[2i]
• The right child of node v, stored in A[i], is stored in
A[2i+1]
Maintaining The Heap Order

In order to maintain the heap order
the value of all children of A[i] must
be less than the value of A[i].

   A[i].child.value <= A[i].value

To keep this property we use a function
called Heapify()
Heapify()
– Input: an array A and an index i.

– Assumption:
• the binary trees rooted at left and right
children of A[i] are heaps
• A[i] may violate the heap order property

– Purpose: Push the value at A[i] down
the heap until the tree rooted at A[i] is
a heap.
The Algorithm
Heapify(A, i)
l  Left_child(i)
r  Right_child(i)
if (l ≤ heap-size[A] and A [l] > A [i] then
Largest  l
else
Largest  i
if (r ≤ heap-size[A] and A[r] > A [largest] then
Largest  r
if (largest = i) then
swap(A [i], A [largest]) A [i]  A [largest]
Heapify(A, largest)
4 < 14 so we need to swap
Now Check 4’s New Children and
Swap
The Heap Property Is Now
Maintained
Heap Sort

1. Build_heap(A)
2. For i  length[A] down to 2 do
3. Swap(A[1], A[i])
4. heap-size[A]  heap-size[A]-1
5. Heapify(A,1)
Time Cost Analysis

Line 1 takes O(n) time

There are n-1 calls to Heapify each call
requires O(log n) time.

Total cost O(n log n).
Example
19

12                 16

1         4        7

Array A

19 12 16 1 4 7
Example
Take out biggest
19

12                 16
Move the last element
to the root

1         4        7

Sorted:
Array A

12 16 1 4 7                        19
Example

7
swap
HEAPIFY()
12             16

1         4

Sorted:
Array A

7 12 16 1 4                         19
Example

16

12             7

1         4

Sorted:
Array A

16 12 7 1 4                  19
Example
Take out biggest
16
Move the last element
to the root
12               7

1             4

Sorted:
Array A

12 7 1 4                  16 19
Example
4

12             7

1

Sorted:
Array A

4 12 7 1              16 19
Example

swap             4

HEAPIFY()
12             7

1

Sorted:
Array A

4 12 7 1              16 19
Example

12

4             7

1

Sorted:
Array A

12 4 7 1              16 19
Example
Take out biggest
12
Move the last
element to the
root                 4             7

1

Sorted:
Array A

4 7 1              12 16 19
Example

1
swap
HEAPIFY()
4             7

Sorted:
Array A

1 4 7                12 16 19
Example
7

4             1

Sorted:
Array A

7 4 1         12 16 19
Example
Take out biggest
7
Move the last
element to the
4             1     root

Sorted:
Array A

1 4               7 12 16 19
Example

swap             1

HEAPIFY()
4

Sorted:
Array A

4 1       7 12 16 19
Example
Take out biggest
Move the last                                     4
element to the
root
1

Sorted:
Array A

1      4 7 12 16 19
Example
Take out biggest
1

Sorted:
Array A

1 4 7 12 16 19
Example

Sorted:

1 4 7 12 16 19

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 58 posted: 9/2/2010 language: English pages: 30
How are you planning on using Docstoc?