Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann."— Presentation transcript:

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

2 Michael Eckmann - Skidmore College - CS 206 - Spring 2009 Today’s Topics Questions? Comments? Quicksort Selection problem

3 Quicksort algorithm 1) if size of list, L is 0 or 1, return 2) pick some element in list as a pivot element 3) divide the remaining elements (minus the pivot) of L into two groups, L 1, those with elements less than the pivot, and L 2, those with elements greater than or equal to the pivot 4) return (Quicksort(L 1 ) followed by pivot, followed by Quicksort(L 2 ))‏ Quicksort

4 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

5 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 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

6 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

7 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

8 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 3 rd 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 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


Download ppt "CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann."

Similar presentations


Ads by Google