ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

20
ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran

description

3 The heap property: A[parent(i)]  A[i] Height of a node Max distance from it to a descendant leaf

Transcript of ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

Page 1: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

ECOE 556: Algorithms and Computational Complexity

Heapsort

Serdar Taşıran

Page 2: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

2

Heapsort Sorts array A[1..n] in place

At most a constant amount of storage outside the array Sorts in O(n lg n) time Useful data structure “heap”

Page 3: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

3

The heap property: A[parent(i)] A[i]

Height of a node Max distance from it to a descendant leaf

Page 4: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

4

Basic procedures on heaps MAX-HEAPIFY

Runs in O(lg n) time. Needed for maintaining the heap property

BUILD-MAX-HEAP Linear time Produces max-heap from unordered input array

HEAPSORT O(n lg n) Sorts array in place

MAX-HEAP-INSERT HEAP-EXTRACT-MAX HEAP-INCREASE-KEY HEAP-MAXIMUM

Page 5: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

5

Binary trees rooted at LEFT(i) and RIGHT(i) are max-heaps.

A[i] may be smaller than LEFT(i) and/or RIGHT(i)

Page 6: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

6

MAX-HEAPIFY EXAMPLE

Page 7: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

7

Analysis of MAX-HEAPIFY Running time One subtree has size at most 2n/3

Since the heap is an almost full binary tree T(n) T(2n/3) + (1) Solution: T(n) = O(lg n) Or, T(n) = O(h)

Page 8: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

8

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Produces max-heap from unordered input array What is the running time?

Page 9: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

9

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Produces max-heap from unordered input array Running time: T(n) = O(n)

Sum over all i n-element heap

FACT: height = floor(lg n) FACT: # of elements at height h ceil(n / 2h+1)

[Needs to be proven]

Page 10: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

10

Page 11: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

11

Proving BUILD-MAX-HEAP correctLOOP INVARIANT: At the start of each iteration of the for loop, each node

i+1, i+2, …, n is the root of a max-heap

Page 12: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

12

Page 13: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

13

Page 14: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

14

Priority queues A data structure for maintaining a set S of elements

Each has an associated key Supported operations

INSERT(S,x) MAXIMUM(S) EXTRACT-MAX(S) INCREASE-KEY(S,x,k)

Page 15: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

15

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 16: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

16

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 17: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

17

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 18: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

18

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 19: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

19

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 20: ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.

20

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.