Heap And Heap Sort

Post on 25-Feb-2016

82 views 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 of Heap And Heap Sort

Heap And Heap Sort

Ethan Coulter4/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 storedin the root

Max Heap Example

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]

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()

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.

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)

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 do3. Swap(A[1], A[i])4. heap-size[A] heap-size[A]-15. Heapify(A,1)

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

Example

19

12 16

41 7

19 12 16 1 4 7

Array A

Example

19

12 16

41 7

1912 16 1 4 7

Array ASorted:

Take out biggest

Move the last elementto the root

Example

12 16

41

7

1912 16 1 47

Array ASorted:

HEAPIFY()swap

Example

12

16

41

7

191216 1 47

Array ASorted:

Example

12

16

41

7

1912 161 47

Array ASorted:

Take out biggest

Move the last elementto the root

Example

12

4

1

7

1912 1614 7

Array ASorted:

Example

12

4

1

7

1912 1614 7

Array ASorted:

HEAPIFY()

swap

Example

12

4

1

7

1912 1614 7

Array ASorted:

Example

12

4

1

7

1912 1614 7

Array ASorted:

Take out biggest

Move the lastelement to the root

Example

4

1

7

1912 161 4 7

Array ASorted:

HEAPIFY()swap

Example

4 1

7

1912 16147

Array ASorted:

Example

4 1

7

1912 161 4 7

Array ASorted:

Take out biggest

Move the lastelement to the root

Example

4

1

1912 1614 7

Array ASorted:

HEAPIFY()

swap

Example

4

1

1912 161 4 7

Array ASorted:

Take out biggestMove the lastelement to the root

Example

1

1912 161 4 7

Array ASorted:

Take out biggest

Example

1912 161 4 7

Sorted: