ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.
-
Upload
dylan-butler -
Category
Documents
-
view
216 -
download
0
description
Transcript of ECOE 556: Algorithms and Computational Complexity Heapsort Serdar Taşıran.
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”
3
The heap property: A[parent(i)] A[i]
Height of a node Max distance from it to a descendant leaf
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
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
MAX-HEAPIFY EXAMPLE
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
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
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
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
12
13
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
16
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
17
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
18
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
19
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
20
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.