UE CS 215  Lecture 37 (18 pages)
Previewing pages 1, 2, 3, 4, 5, 6 of 18 page document View the full content.Lecture 37
Previewing pages 1, 2, 3, 4, 5, 6 of actual document.
View the full content.View Full Document
Lecture 37
0 0 99 views
 Pages:
 18
 School:
 University of Evansville
 Course:
 Cs 215  Fundamentals of Programming II
Fundamentals of Programming II Documents

18 pages

24 pages

C++ Programming Style Guideline
7 pages

25 pages

CS 215 – Fundamentals of Programming II
4 pages

6 pages

Tips on Managing Programming Complexity
5 pages

CS 215 Fundamentals of Programming II
11 pages

CS 215 Fundamentals of Programming II Project 3
3 pages

14 pages

18 pages

22 pages

CS 215 Fundamentals of Programming II
5 pages

22 pages

3 pages

CS 215 Fundamentals of Programming II
6 pages

CS 215 Fundamentals of Programming II Project 2
3 pages

18 pages

15 pages

18 pages

19 pages

17 pages

CS 215 Fundamentals of Programming II
6 pages

18 pages

10 pages

17 pages

CS 215 Fundamentals of Programming II
5 pages

2 pages

6 pages

19 pages

CS 215 Fundamentals of Programming II
11 pages

CS 215 Fundamentals of Programming II
6 pages

CS 215 Fundamentals of Programming II
6 pages

CS 215 Fundamentals of Programming II
6 pages

CS 215 – Fundamentals of Programming II  Project 3
3 pages

CS 215 Fundamentals of Programming II
4 pages

21 pages

28 pages

CS 215  Fundamentals of Programming II  Project 2
3 pages

Analysis and Design Style Guideline
8 pages

17 pages

25 pages

CS 215 Fundamentals of Programming II
5 pages

4 pages

16 pages

15 pages

CS 215 – Fundamentals of Programming II
8 pages

14 pages

CS 215 Fundamentals of Programming
7 pages

CS 215  Fundamentals of Programming II
3 pages

CS 215 – Fundamentals of Programming II
4 pages

CS 215 – Fundamentals of Programming I
3 pages

CS 215 Fundamentals of Programming II
9 pages

20 pages

23 pages

13 pages

5 pages

CS 215  Fundamentals of Programming I
9 pages

CS 215 – Fundamentals of Programming II
6 pages

Tips on Managing Programming Complexit
5 pages

18 pages

CS 215 Fundamentals of Programming II Project 7
6 pages

Project 1  CS 215  Fundamentals of Programming I
5 pages

CS 215 Fundamentals of Programming II Project 4
10 pages

CS 215 Fundamentals of Programming II Project 3
4 pages

18 pages

CS 215 Fundamentals of Programming II
3 pages

28 pages

CS 215 Fundamentals of Programming II
6 pages

19 pages

CS 215 Fundamentals of Programming II
6 pages

18 pages

14 pages

CS 215 – Fundamentals of Programming II
6 pages

2 pages

17 pages

22 pages

CS 215 Fundamentals of Programming I
7 pages

CS 215 Fundamentals of Programming II
5 pages

18 pages
Sign up for free to view:
 This document and 3 million+ documents and flashcards
 High quality study guides, lecture notes, practice exams
 Course Packets handpicked by editors offering a comprehensive review of your courses
 Better Grades Guaranteed
Lecture 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 1 Outline More Sorting Quicksort 2nd half of Section 13 2 More algorithm analysis O nlog2n running time Worst case average case Wednesday April 13 CS 215 Fundamentals of Programming II Lecture 37 2 Sorting Recall Applications often require data to be sorted For a vector v with n elements this means the elements are arranged such that v 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 n 2 algorithms Today we will look at a fast algorithm Wednesday April 13 CS 215 Fundamentals of Programming II Lecture 37 3 Divide 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 4 Quicksort 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 5 Quicksort 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 6 Quicksort 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 7 Partition 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 8 Partition 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 9 Partition 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 10 Partition 0 1 2 3 4 5 6 7 8 9 10 v 40 20 10 80 60 50 pivot too big index v v v 7 7 7 too small index 80 100 90 70 6 too small index 50 60 80 100 90 70 4 5 too small index 20 10 30 40 50 60 80 100 90 70 4 Wednesday April 13 7 4 40 20 10 30 too big index 30 100 90 70 3 40 20 10 30 60 50 too big index 7 pivot index CS 215 Fundamentals of Programming II Lecture 37 11 Partition 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 1 2 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 index 4 Set v first to v pivot index 5 Set v pivot index to pivot Wednesday April 13 CS 215 Fundamentals of Programming II Lecture 37 12 In 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 Fundamentals of Programming II Lecture 37 13 Analysis of Quicksort Assume that n is power of 2 so that n 2 k and k log2n and that pivot is always in the middle of the range so each sublist is equal in size At partition level 0 1 list of size n results in n comparisons creating 2 sublists of size n 2 At partition level 1 2 sublists of size n 2 results in 2 n 2 comparisons creating 4 sublists of size n 4 At partition level 2 4 sublists of size n 4 results in 4 n 4 …
View Full Document