110 likes | 124 Views
Algorithms CSCI 235 , Fall 2019 Lecture 15 Analysis of Heap Sort. 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.
E N D
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.
Pseudocode for Heapsort Heapsort(A) Build-Max-Heap(A) for i= A.lengthdownto 2 Swap(A, 1, i) A.heap-size = A.heap-size - 1 Max-Heapify (A, 1) What is the running time of Heapsort? Must find running times of Build-Heap and Heapify first.
Running time of Heapify Maximum number of swaps in Heapify is the height of the heap. 1 Height of n element heap is: 3 2 5 6 7 4 8 9 10 Therefore, the running time of heapify is O(lgn)
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
Cost of Heap-Sort Heapsort(A) Build-Max-Heap(A) for i= A.lengthdownto 2 Swap(A, 1, i) A.heap-size = A.heap-size - 1 Max-Heapify (A, 1) T(n) = ?
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.
Extracting the Maximum Idea: Extract the maximum element, reduce the heap size by 1, then heapify. Heap-Extract-Max(A) ifA.heap_size < 1 error "heap underflow" max = A[1] // root value is max A[1] = A[A.heap_size] // put A[A.heap_size] in root A.heap_size=A.heap_size - 1 // decrease heap size Max-Heapify(A, 1) // enforce heap property return max
Example 1 16 We will work this through in class. 3 12 8 5 4 2 3
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. MaxHeapInsert(A, key) A.heap_size=A.heap_size + 1 // increase size of heap i=A.heap-size while i > 1 and A[Parent(i)] < key A[i] = A[Parent(i)] i= Parent(i) A[i] = key
Example Insert the number, 15. We will work through this in class. 1 16 3 14 10 5 6 7 4 3 9 7 8 8 9 10 2 4 1