DOC PREVIEW
UE CS 215 - Lecture 37

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 37OutlineSortingDivide and ConquerQuicksort 1Quicksort 2Quicksort 3Partition 1Partition 2Partition 3Partition 4Partition 5In-class ExerciseAnalysis of Quicksort 1Analysis of Quicksort 2Analysis of Quicksort 3Choosing a Pivot 1Choosing a Pivot 2Wednesday, April 13 CS 215 Fundamentals of Programming II - Lecture 37 1Lecture 37Log into Linux. We will be adding code to files sort.h and sort-examples.cpp from last class. Reminder: Homework 13 due today. Reminder: Project 7 due on Friday. No class on Friday. CS Senior Project presentations 1:00-2:30 or so in KC-267. Office hours 3-4:30.Project 8 posted, includes material to be covered next week, due last day of class.Questions?Wednesday, April 13 CS 215 Fundamentals of Programming II - Lecture 37 2OutlineMore SortingQuicksort - 2nd half of Section 13.2More algorithm analysisO(nlog2n) running timeWorst-case != average-caseWednesday, April 13 CS 215 Fundamentals of Programming II - Lecture 37 3SortingRecall: Applications often require data to be sorted. For a vector v with n elements this means the elements are arranged such thatv[0]  v[1]  ...  v[n-2]  v[n-1]There are lots of different algorithms. Last time we look at a couple of "slow", O(n2) algorithms. Today we will look at a "fast" algorithm.Wednesday, April 13 CS 215 Fundamentals of Programming II - Lecture 37 4Divide and ConquerOne class of recursive algorithms is called divide and conquer.The basic idea is to divide the problem in two or more approximately equal-sized subproblems, solve them directly or recursively, then combine the subproblem results into the result for the original problem.Section 13.2 covers two sorting algorithms, mergesort and quicksort that use this method. We will cover only quicksort.Wednesday, April 13 CS 215 Fundamentals of Programming II - Lecture 37 5QuicksortNote: the textbook presents a method for sorting arrays that uses pointer arithmetic. We will be sorting vectors, so we will be using indexes [first, last) to indicate the range to be sorted.One of the reasons that sorting algorithms like selection and insertion sort are so slow is that the exchanges do not move the elements very far. Quicksort attempts to speed this up by making the exchanges more effective.Wednesday, April 13 CS 215 Fundamentals of Programming II - Lecture 37 6QuicksortThe basic idea of quicksort is to arrange the elements of a vector range [first, last) into two "sublists" such that:values in lower sublist  pivot value < values in higher sublistThen recursively apply this idea to each sublist until get to base cases of 0 or 1 elements in a sublist.Wednesday, April 13 CS 215 Fundamentals of Programming II - Lecture 37 7QuicksortThe Quicksort function itself receives and passes back a vector, v, and receives first and last indexes representing range [first, last). Its algorithm is:1. If last - first > 1 then // at least 2 elements 1.1 Partition the range [first, last) using Partition (v, first, last, pivotIndex) 1.2 Sort the left sublist using Quicksort (v, first, pivotIndex) 1.3 Sort the right sublist using Quicksort (v, pivotIndex+1, last)Wednesday, April 13 CS 215 Fundamentals of Programming II - Lecture 37 8PartitionDividing the original vector range into the two sublists is encapsulated in the Partition function. This function receives and passes back a vector, v, and receives indexes first and last representing range [first, last). It passes back pivot_index, the index where the pivot element is placed.The first step is to choose a pivot element. For ease of implementation, we will choose the first element of the range (i.e., v[first]).Wednesday, April 13 CS 215 Fundamentals of Programming II - Lecture 37 9PartitionTo divide the range, we scan from the left end (using too_big_index) looking for an element that is greater than the pivot, and scan from the right end (using too_small_index) looking for an element that is less than or equal to the pivot, then swap them. The scanning and swapping continues until the indexes cross each other. When the indexes cross, too_small_index is the index of the rightmost element that is less than or equal to the pivot element.Wednesday, April 13 CS 215 Fundamentals of Programming II - Lecture 37 10PartitionFinally, to place the pivot element (v[first]), it is swapped with the element at too_small_index.The next slide shows an example of how this works for the first partition of an original call: Quicksort(v, 0, v.size());The green circled element is the pivot. The red circled elements are the ones found to be in the "wrong half" and are swapped. pivot_index is set to the index where the pivot element is placed.Wednesday, April 13 CS 215 Fundamentals of Programming II - Lecture 37 11Partition40 20 10 80 60 50 7 30 100 90 70[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]v40 20 10 30 60 50 7 80 100 90 70v40 20 10 30 7 50 60 80 100 90 70v7 20 10 30 40 50 60 80 100 90 70vpivottoo_big_index3too_small_index74too_small_index6too_small_index45pivot_index4too_big_indextoo_big_indexWednesday, April 13 CS 215 Fundamentals of Programming II - Lecture 37 12PartitionThe algorithm for Partition( ) is as follows:1. Initialize pivot to v[first], too_big_index to first+1, and too_small_index to last-12. While the indexes have not crossed 2.1 While too_big_index has not reached last and v[too_big_index] is less than or equal to the pivot 2.1.1 Increment too_big_index 2.2 While v[too_small_index] is greater than the pivot 2.2.1 Decrement too_small_index 2.3 If the indexes have not met or crossed then 2.3.1 Swap v[too_big_index] and v[too_small_index]3. Set pivot_index to too_small_index4. Set v[first] to v[pivot_index]5. Set v[pivot_index] to pivotWednesday, April 13 CS 215 Fundamentals of Programming II - Lecture 37 13In-class ExerciseImplement Partition( ) and Quicksort( ) as a function templates in file sort.h. In addition, write QuicksortStart( ), a wrapper template function that receives and passes back a vector, v, (like the other sorting functions) and makes the original call to start the recursion: Quicksort (v, 0, v.size());In file sort-examples.cpp, add code in the main program to make a new copy of the data vector, call the QuicksortStart function, and print out the sorted vector.Wednesday, April 13 CS 215


View Full Document

UE CS 215 - Lecture 37

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download Lecture 37
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 37 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 37 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?