Lecture 41 Log into Linux. We will finish Heapsort in files sort.h - - PowerPoint PPT Presentation

lecture 41
SMART_READER_LITE
LIVE PREVIEW

Lecture 41 Log into Linux. We will finish Heapsort in files sort.h - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 1

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?

slide-2
SLIDE 2

Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 2

Outline

 Finish Heapsort  More analysis  O(nlog2n)  Comparison of sorting algorithms

slide-3
SLIDE 3

Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 3

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.

slide-4
SLIDE 4

Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 4

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)

slide-5
SLIDE 5

Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 5

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.

slide-6
SLIDE 6

Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 6

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

slide-7
SLIDE 7

Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 7

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.

slide-8
SLIDE 8

Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 8

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

  • f the tree to the root = depth of tree = O(log2n)

 Thus the worst-case running time is: O(n) times

O(log2n) = O(nlog2n).

slide-9
SLIDE 9

Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 9

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(log2n)

slide-10
SLIDE 10

Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 10

Analysis of Heapsort

 Finally, look at Heapsort

 MakeHeap is O(nlog2n)  Loop runs from n to 1 => O(n) times; each iteration

calls ReheapifyDown => O(log2n); results in O(nlog2n)

 O(nlog2n) + O(nlog2n) = O(2nlog2n) = O(nlog2n)

 Thus Heapsort is an O(nlog2n) algorithm.

slide-11
SLIDE 11

Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 11

Comparison of Sorting Algorithms

 Most sorting algorithms that are based on

swapping elements can be classified into one of the following categories.

O(n2) O(nlog2n) Selection Sort – Successively find the ith element and put it in the ith place. Does no better on nearly sorted vectors SelectionSort Heapsort Exchange Sort – Systematically compare two elements and exchange them if in the wrong order BubbleSort – very inefficient, except for nearly sorted vectors Quicksort – very efficient in average case; bad for almost sorted vectors Insertion Sort – Consider each element and insert it into its correct place relative to already sorted elements Linear InsertionSort – very efficient for nearly sorted vectors Binary InsertionSort – use binary search to find place

slide-12
SLIDE 12

Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 12

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.

slide-13
SLIDE 13

Wednesday, December 1 CS 215 Fundamentals of Programming II - Lecture 40 13

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.