cs 206 introduction to computer science ii 04 / 28 / 2009 instructor: michael eckmann

8
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann

Upload: roy-park

Post on 18-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann

CS 206Introduction to Computer Science II

04 / 28 / 2009

Instructor: Michael Eckmann

Page 2: CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 206 - Spring 2009

Today’s Topics• Questions? Comments?

• Quicksort

• Selection problem

Page 3: CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann

• Quicksort algorithm1) if size of list, L is 0 or 1, return2) pick some element in list as a pivot element3) divide the remaining elements (minus the pivot) of L into two groups,

L1, those with elements less than the pivot, and

L2, those with elements greater than or equal to the pivot

4) return (Quicksort(L1) followed by pivot, followed by Quicksort(L

2))

Quicksort

Page 4: CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann

• To pick some element in list as a pivot element we can either– a good way is to pick the pivot is the median of 3 elements (say the

median of the first, middle and last element) • not much extra work• the almost sorted case isn't a problem for this

Quicksort

Page 5: CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann

• Divide strategy• 1) swap the pivot with the last in the list• 2) start index i pointing to first in list and index j to next to last element• 3) while (element at i < pivot)

increment i • 4) while (element at j >= pivot)

decrement j

• 5) if (i < j) element at i is > pivot and element at j is < pivot so, we swap

them and repeat from step 3.

• 6) when i > j, we swap the pivot that is in the last place with the element at i.

Quicksort

Page 6: CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann

• Let's write Quicksort

– We can make quicksort be a recursive method that takes in

• an array

• the starting index of the data to be sorted

• the number of elements of the data to be sorted

– quicksort will call a method to partition the elements

• find a pivot and divide a (portion of a) list into elements less than pivot, followed by pivot, followed by elements greater than pivot

• This method will take in – an array – the starting index of the data to be sorted– the number of elements of the data to be sorted

• and it will return the pivot index and alter the order of the elements of a subset of the array passed in

Quicksort

Page 7: CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann

• A typical speedup for Quicksort is to do the following:– when we get down to some small number of elements (say 10) in our list,

instead of using quicksort on them, we do insertion sort.– Let's use that xSort applet to visualize insertion sort.

• How would we alter the code we just wrote to do insertion sort when the

number of elements to sort is small?

Quicksort

Page 8: CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann

• Let's discuss a similar algorithm that solves the Selection problem. • The selection problem is the desire to find the kth smallest item in an

unsorted list. • (e.g. in a list indexed from 0 to 99, if we wanted to find the 3rd smallest item,

k=3 and if our list was sorted (which it isn't) this item would live at index 2)• We can use the partition ideas from quicksort. • If the pivot happens to live at index k-1, we're done. If not, then focus only

on the side of the pivot that k is on. That is, if pivot happens to be at index j after partitioning, then if k-1 < j only work on the left list, if k-1>j then only work on the right list and if k-1==j, then we're done.

• Let's see if we can code this now.

Selection problem