Heap And Heap Sort - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Heap And Heap Sort

Description:

Heap And Heap Sort Ethan Coulter 4/22/2004 CS 146 Dr. Lee What is a heap? A Complete Binary Tree A Complete Binary Tree is a binary tree that is completely filled ... – PowerPoint PPT presentation

Number of Views:280
Avg rating:3.0/5.0
Slides: 31
Provided by: Etha72
Category:
Tags: heap | sort

less

Transcript and Presenter's Notes

Title: Heap And Heap Sort


1
Heap And Heap Sort
  • Ethan Coulter
  • 4/22/2004
  • CS 146 Dr. Lee

2
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.

3
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.

4
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

5
Max Heap Example
6
Properties Of The Heap
  • The parent of node v, stored in Ai, is
  • stored in Ai/2
  • The left child of node v, stored in Ai, is
    stored in A2i
  • The right child of node v, stored in Ai, is
    stored in A2i1

7
Maintaining The Heap Order
  • In order to maintain the heap order
  • the value of all children of Ai must
  • be less than the value of Ai.
  • Ai.child.value lt Ai.value
  • To keep this property we use a function called
    Heapify()

8
Heapify()
  • Input an array A and an index i.
  • Assumption
  • the binary trees rooted at left and right
  • children of Ai are heaps
  • Ai may violate the heap order property
  • Purpose Push the value at Ai down
  • the heap until the tree rooted at Ai is
  • a heap.

9
The Algorithm
  • Heapify(A, i)
  • l ? Left_child(i)
  • r ? Right_child(i)
  • if (l heap-sizeA and A l gt A i then
  • Largest ? l
  • else
  • Largest ? i
  • if (r heap-sizeA and Ar gt A largest then
  • Largest ? r
  • if (largest i) then
  • swap(A i, A largest) A i ? A largest
  • Heapify(A, largest)

10
4 lt 14 so we need to swap
11
Now Check 4s New Children and Swap
12
The Heap Property Is Now Maintained
13
Heap Sort
  • 1. Build_heap(A)
  • 2. For i ? lengthA down to 2 do
  • 3. Swap(A1, Ai)
  • 4. heap-sizeA ? heap-sizeA-1
  • 5. Heapify(A,1)

14
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).

15
Example
19
12
16
1
4
7
Array A
19
12
16
1
4
7
16
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
17
Example
7
swap
HEAPIFY()
12
16
1
4
Sorted
Array A
12
16
1
4
7
19
18
Example
16
12
7
1
4
Sorted
Array A
12
16
1
4
7
19
19
Example
Take out biggest
16
Move the last element to the root
12
7
1
4
Sorted
Array A
12
1
4
7
19
16
20
Example
4
12
7
1
Sorted
Array A
12
1
4
7
19
16
21
Example
4
swap
HEAPIFY()
12
7
1
Sorted
Array A
12
1
4
7
19
16
22
Example
12
7
4
1
Sorted
Array A
12
1
4
7
19
16
23
Example
Take out biggest
12
Move the last element to the root
7
4
1
Sorted
Array A
1
4
7
19
12
16
24
Example
1
swap
HEAPIFY()
7
4
Sorted
Array A
1
4
7
19
12
16
25
Example
7
4
1
Sorted
Array A
1
4
7
19
12
16
26
Example
Take out biggest
7
Move the last element to the root
4
1
Sorted
Array A
1
4
19
12
16
7
27
Example
1
swap
HEAPIFY()
4
Sorted
Array A
4
1
19
12
16
7
28
Example
Take out biggest
4
Move the last element to the root
1
Sorted
Array A
1
4
19
12
16
7
29
Example
Take out biggest
1
Sorted
Array A
1
4
19
12
16
7
30
Example
Sorted
19
12
16
1
4
7
Write a Comment
User Comments (0)
About PowerShow.com