1 / 11

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

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.

mysliwiec
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AlgorithmsCSCI 235, Fall2019Lecture 15Analysis 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.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.

  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 6 7 4 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.lengthdownto 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) 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

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

  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. 1 16 3 14 10 5 6 7 4 3 9 7 8 8 9 10 2 4 1

More Related