Download presentation
Presentation is loading. Please wait.
Published byMarlene Rice Modified over 9 years ago
1
1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort
2
2 Heap Sort In English: Build a Heap out of the elements of the array We know that the maximum value is at A[1], so we swap it with the last element of the heap. Reduce the size of the heap by 1. Heapify the new, smaller heap. Repeat this process until all the nodes have been sorted.
3
3 Pseudocode for Heapsort Heapsort(A) Build-Heap(A) for i length[A] downto 2 do Swap(A, 1, i) heap-size[A] heap-size[A] - 1 Heapify(A, 1) What is the running time of Heapsort? Must find running times of Build-Heap and Heapify first.
4
4 Recall Heapify Heapify( ) is a function that creates a heap from a node i and two subtrees that are already heaps. 16 314 15 6 9 11 782 1 i=2 3 4 5 67 89 10 Example: i = 2 Swap the value at node i with the largest of the values at child nodes. If value at node i is larger than those of children, stop. Otherwise, continue with the next level.
5
5 Running time of Heapify Maximum number of swaps in Heapify is the height of the heap. 1 3 4 5 67 89 10 2 Height of n element heap is: Therefore, the running time of heapify is O(lgn)
6
6 Running time of Build heap Recall that Build-Heap starts with the last element and repeatedly calls Heapify to create heaps from the bottom up. If the height of a node is given by the longest distance from a leaf to that node, then the number of nodes at a given height h is at most: Why? Build-Heap calls heapify for each node at a given height: T(n) = ? number of nodes at height h cost of Heapify at height h
7
7 Cost of Heap-Sort Heapsort(A) Build-Heap(A) for i length[A] downto 2 do Swap(A, 1, i) heap-size[A] heap-size[A] - 1 Heapify(A, 1) T(n) = ?
8
8 Using a Heap in Priority Queues A priority queue is a queue in which the element with the highest value is retrieved first. WIPO: Whatever in, Priority out. Priority Queue Operations: Insert(S, x) Inserts element x into set S Maximum(S) Returns the element of S with the largest value (or key) Extract-Max(S) Removes and returns the element of S with the largest value. Heaps are useful data structures for priority queue functions.
9
9 Extracting the Maximum Idea: Extract the maximum element, reduce the heap size by 1, then heapify. Heap-Extract-Max(A) if heap_size[A] < 1 then error "heap underflow" max A[1]{ root value is max } A[1] A[heap_size[A]]{ put A[heap_size[A]] in root} heap_size[A] heap_size[A] - 1{ decrease heap size } Heapify(A, 1){ enforce heap property } return max
10
10 Example 16 12 8 3 2 1 3 4 5 We will work this through in class.
11
11 Heap-Insert Idea: To insert an element at the proper place in the heap, traverse the tree from leaf to root and find the correct place. HeapInsert(A, key) heap_size[A] heap_size[A] + 1{ increase size of heap } i heap-size[A] while i > 1 and A[Parent(i)] < key do A[i] A[Parent(i)] i Parent(i) A[i] key
12
12 Example 16 1410 8 7 9 3 241 1 3 4 5 67 89 Insert the number, 15. We will work through this in class.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.