Download presentation
Presentation is loading. Please wait.
Published byDylan Butler Modified over 9 years ago
1
ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran
2
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”
3
3 The heap property: A[parent(i)] A[i] Height of a node Max distance from it to a descendant leaf
4
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
5
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)
6
6 MAX-HEAPIFY EXAMPLE
7
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)
8
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?
9
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]
10
10
11
11 Proving BUILD-MAX-HEAP correct LOOP 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
12
12
13
13
14
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)
15
15 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
16
16 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
17
17 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18
18 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
19
19 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
20
20 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.