Lecture 41 Log into Linux. We will finish Heapsort in files sort.h and sort-examples.cpp from the last class. Reminder: Homework 10 is due at the beginning of class on Monday, no late work accepted. Project 8 is due on Monday at 4:30pm as usual. Final exam review sheet has been posted. Review on Monday. Questions? Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 1
Outline Finish Heapsort More analysis O(nlog 2 n) Comparison of sorting algorithms Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 2
Heap Review: A heap is a complete, binary tree of elements that can be compared using a less- than operator (<). For each interior (non-leaf) node, the node's value is never less than its children's node values. Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 3
Heapsort Review: The heapsort algorithm is Interpret the vector to be sorted as a binary tree. Arrange the elements so that the resulting binary tree is a heap. Repeat the following steps until all values are in their correct places: Swap the root of the heap with its last element and reduce the size of the tree by one. Reheapify the remaining tree by pushing the new root element downward until it reaches its correct place (no children larger than the element) Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 4
MakeHeap Review: The final thing we need is a procedure to convert an arbitrary vector into a heap. The main idea is to "add" node values one at a time to the "end" of the tree and position the new node value by pushing it up the tree until it is larger than its children. This process is called reheapification upwards . The MakeHeap function template receives and passes back a vector, v , that is converted into a heap. Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 5
MakeHeap The algorithm is: 1. For i from 1 to v.size()-1 do 1.1 Initialize k to i 1.2 While k is not yet the root and v[k] is larger than its parent 1.2.1 Swap v[k] with its parent 1.2.2 Set k to the the parent index Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 6
In-class Exercise Finish implementing MakeHeap , ReheapifyDown , and Heapsort as function templates in file sort.h . In file sort-examples.cpp , add code in the main program to make a new copy of the data vector, call the Heapsort function, and print out the sorted vector. Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 7
Analysis of Heapsort First look at the running-time of MakeHeap The outer loop runs from 1 to n => O(n) times. The body of the outer loop consists of "adding" a new element to the heap and pushing it up using a reheapification upward process loop. What is the maximum number of times the inner loop will execute? Length of path from the bottom of the tree to the root = depth of tree = O(log 2 n) Thus the worst-case running time is: O(n) times O(log 2 n) = O(nlog 2 n). Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 8
Analysis of Heapsort Now look at ReheapifyDown Loop moves a value at the root down a path What is the maximum number of times the loop executes? Again length of path from root to bottom level = depth of tree = O(log 2 n) Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 9
Analysis of Heapsort Finally, look at Heapsort MakeHeap is O(nlog 2 n) Loop runs from n to 1 => O(n) times; each iteration calls ReheapifyDown => O(log 2 n); results in O(nlog 2 n) O(nlog 2 n) + O(nlog 2 n) = O(2nlog 2 n) = O(nlog 2 n) Thus Heapsort is an O(nlog 2 n) algorithm. Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 10
Comparison of Sorting Algorithms Most sorting algorithms that are based on swapping elements can be classified into one of the following categories. O(n 2 ) O(nlog 2 n) Selection Sort – Successively find the SelectionSort Heapsort i th element and put it in the i th place. Does no better on nearly sorted vectors Exchange Sort – Systematically BubbleSort – very Quicksort – very compare two elements and exchange inefficient, except for efficient in average them if in the wrong order nearly sorted vectors case; bad for almost sorted vectors Insertion Sort – Consider each element Linear InsertionSort – Binary InsertionSort – and insert it into its correct place relative very efficient for use binary search to to already sorted elements nearly sorted vectors find place Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 11
Comparison of Sorting Algorithms Generally, the faster algorithms have much higher overhead (i.e., the coefficients are larger), so need to have a large vector to make them worth doing. Linear InsertionSort is a good compromise for small vectors (15-20 elements). Low overhead, good with nearly sorted elements. Can combine algorithms. E.g., use Quicksort until the partitions are less than 20 elements, then use InsertionSort. Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 12
Comparison of Sorting Algorithms Summary: there is no one "best" sorting algorithm. Which one to use depends on the amount of data and whether the data might be mostly sorted. Also there are specialized sorting algorithms for specific types that use extra O(n) storage that are faster. E.g. radix sort for integers that uses queues. Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 13
Recommend
More recommend