Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms CSCI 235, Spring 2019 Lecture 15 Analysis of Heap Sort

Similar presentations


Presentation on theme: "Algorithms CSCI 235, Spring 2019 Lecture 15 Analysis of Heap Sort"— Presentation transcript:

1 Algorithms CSCI 235, Spring 2019 Lecture 15 Analysis of Heap Sort

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 Pseudocode for Heapsort
Heapsort(A) Build-Max-Heap(A) for i = A.length downto 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.

4 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 4 6 7 8 9 10 Therefore, the running time of heapify is O(lgn)

5 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

6 Cost of Heap-Sort Heapsort(A) Build-Max-Heap(A)
for i = A.length downto 2 Swap(A, 1, i) A.heap-size = A.heap-size - 1 Max-Heapify (A, 1) T(n) = ?

7 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.

8 Extracting the Maximum
Idea: Extract the maximum element, reduce the heap size by 1, then heapify. Heap-Extract-Max(A) if A.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

9 Example 1 16 We will work this through in class. 3 12 8 5 4 3 2

10 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

11 Example Insert the number, 15. We will work through this in class. 16
3 14 10 5 4 6 7 8 7 9 3 8 9 10 2 4 1


Download ppt "Algorithms CSCI 235, Spring 2019 Lecture 15 Analysis of Heap Sort"

Similar presentations


Ads by Google