heap and heap sort

30
Heap And Heap Sort Ethan Coulter 4/22/2004 CS 146 – Dr. Lee

Upload: thisbe

Post on 25-Feb-2016

82 views

Category:

Documents


10 download

DESCRIPTION

Heap And Heap Sort. Ethan Coulter 4/22/2004 CS 146 – Dr. Lee. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Heap And Heap Sort

Heap And Heap Sort

Ethan Coulter4/22/2004

CS 146 – Dr. Lee

Page 2: Heap And Heap Sort

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.

Page 3: Heap And Heap Sort

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.

Page 4: Heap And Heap Sort

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 storedin the root

Page 5: Heap And Heap Sort

Max Heap Example

Page 6: Heap And Heap Sort

Properties Of The Heap

The parent of node v, stored in A[i], isstored 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]

Page 7: Heap And Heap Sort

Maintaining The Heap Order

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

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

To keep this property we use a function called Heapify()

Page 8: Heap And Heap Sort

Heapify()

– Input: an array A and an index i.

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

– Purpose: Push the value at A[i] downthe heap until the tree rooted at A[i] isa heap.

Page 9: Heap And Heap Sort

The Algorithm

Heapify(A, i)l Left_child(i)r Right_child(i)if (l ≤ heap-size[A] and A [l] > A [i] thenLargest lelseLargest iif (r ≤ heap-size[A] and A[r] > A [largest] thenLargest rif (largest = i) thenswap(A [i], A [largest]) A [i] A [largest]Heapify(A, largest)

Page 10: Heap And Heap Sort

4 < 14 so we need to swap

Page 11: Heap And Heap Sort

Now Check 4’s New Children and Swap

Page 12: Heap And Heap Sort

The Heap Property Is Now Maintained

Page 13: Heap And Heap Sort

Heap Sort

1. Build_heap(A)2. For i length[A] down to 2 do3. Swap(A[1], A[i])4. heap-size[A] heap-size[A]-15. Heapify(A,1)

Page 14: Heap And Heap Sort

Time Cost Analysis

Line 1 takes O(n) time

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

Total cost O(n log n).

Page 15: Heap And Heap Sort

Example

19

12 16

41 7

19 12 16 1 4 7

Array A

Page 16: Heap And Heap Sort

Example

19

12 16

41 7

1912 16 1 4 7

Array ASorted:

Take out biggest

Move the last elementto the root

Page 17: Heap And Heap Sort

Example

12 16

41

7

1912 16 1 47

Array ASorted:

HEAPIFY()swap

Page 18: Heap And Heap Sort

Example

12

16

41

7

191216 1 47

Array ASorted:

Page 19: Heap And Heap Sort

Example

12

16

41

7

1912 161 47

Array ASorted:

Take out biggest

Move the last elementto the root

Page 20: Heap And Heap Sort

Example

12

4

1

7

1912 1614 7

Array ASorted:

Page 21: Heap And Heap Sort

Example

12

4

1

7

1912 1614 7

Array ASorted:

HEAPIFY()

swap

Page 22: Heap And Heap Sort

Example

12

4

1

7

1912 1614 7

Array ASorted:

Page 23: Heap And Heap Sort

Example

12

4

1

7

1912 1614 7

Array ASorted:

Take out biggest

Move the lastelement to the root

Page 24: Heap And Heap Sort

Example

4

1

7

1912 161 4 7

Array ASorted:

HEAPIFY()swap

Page 25: Heap And Heap Sort

Example

4 1

7

1912 16147

Array ASorted:

Page 26: Heap And Heap Sort

Example

4 1

7

1912 161 4 7

Array ASorted:

Take out biggest

Move the lastelement to the root

Page 27: Heap And Heap Sort

Example

4

1

1912 1614 7

Array ASorted:

HEAPIFY()

swap

Page 28: Heap And Heap Sort

Example

4

1

1912 161 4 7

Array ASorted:

Take out biggestMove the lastelement to the root

Page 29: Heap And Heap Sort

Example

1

1912 161 4 7

Array ASorted:

Take out biggest

Page 30: Heap And Heap Sort

Example

1912 161 4 7

Sorted: