5 Heaps
5 Heaps
Heaps
Data Structures & Algorithms
CONTENT
• Introduction
• Basic Operations
• Heap Sort
•Introduction
• Basic Operations
• Heap Sort
14 12 7 10 8 6
0 1 2 3 4 5 6
Data Structures & Algorithms 6
CONTENT
• Introduction
•Basic Operations
• Heap Sort
• Insertion - Example
• Introduction
• Basic Operations
•Heap Sort
• Heap Sort
• Given n elements (in an array A[1..n]) to be sorted
• Recall: max heap
• An array is represented by a complete binary tree, in which the key value
in each node is no smaller than the key values in its children (if any)
• A[1] is the root (suppose the first element of the array is A[1])
• A[i] is parent, so A[2i] is the left child and A[2i+1] is the right child
(if A[0] is the root, so A[2i+1] and A[2i+2]) respectively
• O(n log n) time
• Example
• Introduction
• Basic Operations
• Heap Sort