View by Category

The presentation will start after a short

(15 second) video ad from one of our sponsors.

Hot tip: Video ads won’t appear to registered users who are logged in. And it’s free to register and free to log in!

(15 second) video ad from one of our sponsors.

Hot tip: Video ads won’t appear to registered users who are logged in. And it’s free to register and free to log in!

Loading...

PPT – Heap Sort PowerPoint presentation | free to download - id: 148ae9-ZTZjM

The Adobe Flash plugin is needed to view this content

About This Presentation

Write a Comment

User Comments (0)

Transcript and Presenter's Notes

Heap Sort

A Heap is a Binary Tree

Height of tree longest path from root to

leaf Q(lgn)

A heap is a binary tree satisfying the heap

condition At every node in a heap, the node

value is gt all the values in its subtrees. A

heap with heap_size elements can be represented

as an array segment A1..heap_size

Ordering of Nodes

1

16

Each node is filled in order from left to

right. All rows are filled except, possibly, the

last row.

3

2

14

10

5

6

7

4

3

9

7

8

8

9

10

2

4

1

Address of left child of node i gt 2i Address of

right child of node i gt 2i1 Address of parent

of node i gt

Example Left(4) 8 Right(4)

9 Parent(4) 2

Heap properties

- At every node in the heap, that node's value is

greater than or equal to all the values in the

subtrees of that node. - Heaps can be represented as an array. The

elements in the array occur in the order of their

heap addresses.

A 16 14 10 8 7 9 3 2 4 1

Note that the array is not sorted. Can guarantee

that A1 is the maximum value in the array.

Building a Heap

- To perform a heap sort, we must first build a

heap out of our unsorted array. - To build a heap, we will use the function

heapify, which creates a heap from a node i and

two subtrees that are already heaps.

Heapify

1

Example i 2 Want to move the value, 3, to a

position consistent with the heap property.

16

3

i2

3

14

5

6

7

4

11

9

6

15

8

9

10

7

8

2

- In English
- Compare the value at node i with the values at

each child node. - Swap the value at node i with the largest of

those values. - Repeat with the subtree that ith value was

swapped into. - Note If the value at i is in the correct place,

it will be swapped with itself and the algorithm

is done.

Pseudocode for Heapify

Heapify (A, i) lft lt- Left(i) rt lt-

Right(i) if lft lt heap-sizeA and Alft gt

Ai then largest lt- lft else largest lt-

i if rt lt heap-sizeA and Art gt

Alargest then largest lt- rt if largest ! i

then Swap( A, i, largest ) Heapify (A,

largest)

Final Tree from example

1

16

3

i2

15

14

5

6

7

4

11

9

6

8

8

9

10

7

3

2

Can show that heapify runs in O(lgn) time. (Why?)

Building a Heap

- Each leaf of a binary tree is already a heap of

size 1. - Build-Heap will start with the highest addressed,

non-leaf node and heapify it. It then repeats

with each node addressed 1 less than the previous

node. - In a binary tree, the leaf nodes have addresses
- We want to heapify starting with node

Build-Heap(A) heap-size lt- lengthA for i lt-

lengthA / 2 downto 1 do Heapify(A, i)

Example

A 4 7 16 3 27 9 10 34 1 25

1

4

3

2

7

16

5

6

7

4

10

9

27

3

8

9

10

34

1

25

We will work this out in class.

Heap Sort

- In English
- We know that the maximum value is at A1 in a

heap. - We put that value at the end of the array, by

swapping it with the last element of the heap. - We then reduce the size of the heap by 1, so

that the sorted element is no longer part of the

heap. - Heapify the new, smaller heap (A1 is not

necessarily in the correct position for a heap).

This will put a new maximum at the top. - Repeat this process until all the nodes have

been sorted.

Pseudocode for Heapsort

Heapsort(A) Build-Heap(A) for i lt- lengthA

downto 2 do Swap(A, 1, i) heap-sizeA lt-

heap-sizeA - 1 Heapify(A, 1)

What is the running time of Heapsort? Must find

running times of Build-Heap and Heapify first.

Recall Heapify

Heapify( ) is a function that creates a heap from

a node i and two subtrees that are already heaps.

1

16

Example i 2

3

i2

3

14

5

6

7

4

11

9

6

15

8

9

10

7

8

2

Swap the value at node i with the largest of the

values at child nodes. If value at node i is

larger than those of children, stop. Otherwise,

continue with the next level.

Running time of Heapify

Maximum number of swaps in Heapify is the height

of the heap.

1

Height of n element heap is

3

2

5

6

7

4

8

9

10

Therefore, the running time of heapify is O(lgn)

Running time of Build heap

Recall that Build-Heap starts with the last

element and repeatedly calls Heapify to create

heaps from the bottom up.

If the height of a node is given by the longest

distance from a leaf to that node, then the

number of nodes at a given height h is at most

Why?

Build-Heap calls heapify for each node at a given

height

T(n) ?

number of nodes at height h

cost of Heapify at height h

Cost of Heap-Sort

Heapsort(A) Build-Heap(A) for i lt- lengthA

downto 2 do Swap(A, 1, i) heap-sizeA lt-

heap-sizeA - 1 Heapify(A, 1)

O(n) n nconst nconst nO(lgn)

T(n) O(n) nO(lgn) O(nlgn)

Using a Heap in Priority Queues

A priority queue is a queue in which the element

with the highest value is retrieved first. WIPO

Whatever in, Priority out. Priority Queue

Operations Insert(S, x) Inserts element x into

set S Maximum(S) Returns the element of S with

the largest value (or key) Extract-Max(S) Removes

and returns the element of S with the largest

value. Heaps are useful data structures for

priority queue functions.

Extracting the Maximum

Idea Extract the maximum element, reduce the

heap size by 1, then heapify.

Heap-Extract-Max(A) if heap_sizeA lt 1 then

error "heap underflow" max lt- A1 root

value is max A1 lt- Aheap_sizeA put

Aheap_sizeA in root heap_sizeA lt-

heap_sizeA - 1 decrease heap size

Heapify(A, 1) enforce heap property

return max

Example

1

16

We will work this through in class.

3

12

8

5

4

2

3

Heap-Insert

Idea To insert an element at the proper place in

the heap, traverse the tree from leaf to root and

find the correct place.

HeapInsert(A, key) heap_sizeA lt-

heap_sizeA 1 increase size of heap i lt-

heap-sizeA while i gt 1 and AParent(i) lt key

do Ai lt- AParent(i) i lt-

Parent(i) Ai lt- key

Example

Insert the number, 15. We will work through this

in class.

1

16

3

14

10

5

6

7

4

3

9

7

8

8

9

10

2

4

1

Binary Heap Definition

- Binary heap.
- Almost complete binary tree.
- filled on all levels, except last, where filled

from left to right - Min-heap ordered.
- every child greater than (or equal to) parent

06

14

45

78

18

53

47

77

91

81

84

83

64

99

Binary Heap Properties

- Properties.
- Min element is in root.
- Heap with N elements has height ?log2 N?.

06

N 14Height 3

14

45

78

18

53

47

77

91

81

84

83

64

99

Binary Heaps Array Implementation

- Implementing binary heaps.
- Use an array no need for explicit parent or

child pointers. - Parent(i) ?i/2?
- Left(i) 2i
- Right(i) 2i 1

06

1

14

45

2

3

78

18

53

47

4

5

6

7

77

91

81

84

83

64

99

8

9

10

12

14

11

13

Binary Heap Insertion

- Insert element x into heap.
- Insert into next available slot.
- Bubble up until it's heap ordered.
- Peter principle nodes rise to level of

incompetence

06

14

45

78

18

53

47

77

91

81

84

83

64

99

Binary Heap Insertion

- Insert element x into heap.
- Insert into next available slot.
- Bubble up until it's heap ordered.
- Peter principle nodes rise to level of

incompetence

swap with parent

06

14

45

78

18

53

47

77

91

81

84

83

64

99

Binary Heap Insertion

- Insert element x into heap.
- Insert into next available slot.
- Bubble up until it's heap ordered.
- Peter principle nodes rise to level of

incompetence

swap with parent

06

14

45

78

18

42

47

77

91

81

84

83

64

99

53

Binary Heap Insertion

- Insert element x into heap.
- Insert into next available slot.
- Bubble up until it's heap ordered.
- Peter principle nodes rise to level of

incompetence - O(log N) operations.

stop heap ordered

06

14

42

78

18

45

47

77

91

81

84

83

64

99

53

Binary Heap Decrease Key

- Decrease key of element x to k.
- Bubble up until it's heap ordered.
- O(log N) operations.

06

14

42

78

18

45

47

77

91

81

84

83

64

99

53

Binary Heap Delete Min

- Delete minimum element from heap.
- Exchange root with rightmost leaf.
- Bubble root down until it's heap ordered.
- power struggle principle better subordinate is

promoted

06

14

42

78

18

45

47

77

91

81

84

83

64

99

53

Binary Heap Delete Min

- Delete minimum element from heap.
- Exchange root with rightmost leaf.
- Bubble root down until it's heap ordered.
- power struggle principle better subordinate is

promoted

53

14

42

78

18

45

47

77

91

81

84

83

64

99

06

Binary Heap Delete Min

- Delete minimum element from heap.
- Exchange root with rightmost leaf.
- Bubble root down until it's heap ordered.
- power struggle principle better subordinate is

promoted

exchange with left child

53

14

42

78

18

45

47

77

91

81

84

83

64

99

Binary Heap Delete Min

- Delete minimum element from heap.
- Exchange root with rightmost leaf.
- Bubble root down until it's heap ordered.
- power struggle principle better subordinate is

promoted

exchange with right child

14

53

42

78

18

45

47

77

91

81

84

83

64

99

Binary Heap Delete Min

- Delete minimum element from heap.
- Exchange root with rightmost leaf.
- Bubble root down until it's heap ordered.
- power struggle principle better subordinate is

promoted - O(log N) operations.

stop heap ordered

14

18

42

78

53

45

47

77

91

81

84

83

64

99

Binary Heap Heapsort

- Heapsort.
- Insert N items into binary heap.
- Perform N delete-min operations.
- O(N log N) sort.
- No extra storage.

About PowerShow.com

PowerShow.com is a leading presentation/slideshow sharing website. Whether your application is business, how-to, education, medicine, school, church, sales, marketing, online training or just for fun, PowerShow.com is a great resource. And, best of all, most of its cool features are free and easy to use.

You can use PowerShow.com to find and download example online PowerPoint ppt presentations on just about any topic you can imagine so you can learn how to improve your own slides and presentations for free. Or use it to find and download high-quality how-to PowerPoint ppt presentations with illustrated or animated slides that will teach you how to do something new, also for free. Or use it to upload your own PowerPoint slides so you can share them with your teachers, class, students, bosses, employees, customers, potential investors or the world. Or use it to create really cool photo slideshows - with 2D and 3D transitions, animation, and your choice of music - that you can share with your Facebook friends or Google+ circles. That's all free as well!

For a small fee you can get the industry's best online privacy or publicly promote your presentations and slide shows with top rankings. But aside from that it's free. We'll even convert your presentations and slide shows into the universal Flash format with all their original multimedia glory, including animation, 2D and 3D transition effects, embedded music or other audio, or even video embedded in slides. All for free. Most of the presentations and slideshows on PowerShow.com are free to view, many are even free to download. (You can choose whether to allow people to download your original PowerPoint presentations and photo slideshows for a fee or free or not at all.) Check out PowerShow.com today - for FREE. There is truly something for everyone!

You can use PowerShow.com to find and download example online PowerPoint ppt presentations on just about any topic you can imagine so you can learn how to improve your own slides and presentations for free. Or use it to find and download high-quality how-to PowerPoint ppt presentations with illustrated or animated slides that will teach you how to do something new, also for free. Or use it to upload your own PowerPoint slides so you can share them with your teachers, class, students, bosses, employees, customers, potential investors or the world. Or use it to create really cool photo slideshows - with 2D and 3D transitions, animation, and your choice of music - that you can share with your Facebook friends or Google+ circles. That's all free as well!

For a small fee you can get the industry's best online privacy or publicly promote your presentations and slide shows with top rankings. But aside from that it's free. We'll even convert your presentations and slide shows into the universal Flash format with all their original multimedia glory, including animation, 2D and 3D transition effects, embedded music or other audio, or even video embedded in slides. All for free. Most of the presentations and slideshows on PowerShow.com are free to view, many are even free to download. (You can choose whether to allow people to download your original PowerPoint presentations and photo slideshows for a fee or free or not at all.) Check out PowerShow.com today - for FREE. There is truly something for everyone!

presentations for free. Or use it to find and download high-quality how-to PowerPoint ppt presentations with illustrated or animated slides that will teach you how to do something new, also for free. Or use it to upload your own PowerPoint slides so you can share them with your teachers, class, students, bosses, employees, customers, potential investors or the world. Or use it to create really cool photo slideshows - with 2D and 3D transitions, animation, and your choice of music - that you can share with your Facebook friends or Google+ circles. That's all free as well!

For a small fee you can get the industry's best online privacy or publicly promote your presentations and slide shows with top rankings. But aside from that it's free. We'll even convert your presentations and slide shows into the universal Flash format with all their original multimedia glory, including animation, 2D and 3D transition effects, embedded music or other audio, or even video embedded in slides. All for free. Most of the presentations and slideshows on PowerShow.com are free to view, many are even free to download. (You can choose whether to allow people to download your original PowerPoint presentations and photo slideshows for a fee or free or not at all.) Check out PowerShow.com today - for FREE. There is truly something for everyone!

For a small fee you can get the industry's best online privacy or publicly promote your presentations and slide shows with top rankings. But aside from that it's free. We'll even convert your presentations and slide shows into the universal Flash format with all their original multimedia glory, including animation, 2D and 3D transition effects, embedded music or other audio, or even video embedded in slides. All for free. Most of the presentations and slideshows on PowerShow.com are free to view, many are even free to download. (You can choose whether to allow people to download your original PowerPoint presentations and photo slideshows for a fee or free or not at all.) Check out PowerShow.com today - for FREE. There is truly something for everyone!

Recommended

«

/ »

Page of

«

/ »

Promoted Presentations

Related Presentations

Page of

Page of

CrystalGraphics Sales Tel: (800) 394-0700 x 1 or Send an email

Home About Us Terms and Conditions Privacy Policy Contact Us Send Us Feedback

Copyright 2017 CrystalGraphics, Inc. — All rights Reserved. PowerShow.com is a trademark of CrystalGraphics, Inc.

Copyright 2017 CrystalGraphics, Inc. — All rights Reserved. PowerShow.com is a trademark of CrystalGraphics, Inc.

The PowerPoint PPT presentation: "Heap Sort" is the property of its rightful owner.

Do you have PowerPoint slides to share? If so, share your PPT presentation slides online with PowerShow.com. It's FREE!