1 / 18

Announcements

Announcements. P2 is due today Prelim on Monday in class -- Review Session tomorrow -- Bring Your Questions! No quiz today. :-). Today’s Topics. Review Sort Selection Sort Insertion Sort Quicksort Recursive version

naeva
Download Presentation

Announcements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Announcements • P2 is due today • Prelim on Monday in class-- Review Session tomorrow -- Bring Your Questions! • No quiz today. :-) Lecture 12

  2. Today’s Topics • Review • Sort • Selection Sort • Insertion Sort • Quicksort • Recursive version • A brief introduction to recursion -- we’ll spend a whole lecture on recursion later on in the course • Next week: Iterative version Lecture 12

  3. Review • Linear Search • Binary Search • How fast is binary search? • How does binary search work? Lecture 12

  4. 1 8 3 7 2 4 4 8 3 7 2 1 1 2 3 7 8 4 1 2 3 7 8 4 1 2 3 4 8 7 1 2 3 4 7 8 After step 1 Start After step 2 After step 3 After step 4 After step 5 What’s selection sort? Selection sort works by, at each step, placing the next smallest value into position: At each step, the boldfaced value is the one to put in its place next; it gets swapped with the value that’s in the array element where it belongs. Lecture 12

  5. Show me the algorithm! staticpublicvoid selection Sort(int [ ] b) { int k= 0; //inv: b[0..k-1] is sorted, b[0..k-1] <= b[k..]: while (k < b.length-1) { // Set j so that b[j] is the minimum of b[k..b.length-1] int j= k; int h= k+1; // inv.:b[j] is minimum of b[k..h-1] while (h<b.length) { if (b[h] < b[j]) j= h; h= h+1; } // Swap b[k] with b[j] int t= b[k]; b[k]= b[j]; b[j]= t; k= k+1;} Lecture 12

  6. How fast is selection sort? • How fast is selectionSort? Want a general idea of its speed that is independent of the computer on which it executes. Don’t give time in seconds or milliseconds. • The operation that is performed the most is the array comparison b[h] < b[j]. We take the number of such comparisons performed as a measure of speed. Abbreviate b.length as n . . . Lecture 12

  7. Selection sort analysis Iteration No. times array comparison performed of outer loop during this iteration of outer loop 0 n-1 1 n-2 2 n-3 . . . last 1 So number of comparisons is 1 + 2 + 3 + … +(n-2) + (n-1) = n * (n-1) / 2 = n2/2 - n/2 As n gets large, the term n2 dominates. We say the number if comparisons is proportional to n2 and that this is a quadratic algorithm. Lecture 12

  8. Insertion sort • Intuitively: think about when you have been dealt a hand of cards • Look at each item and insert it into its proper place in what you’ve already sorted so far • Thus, the first part of the array is always sorted, and the last part is unsorted. Lecture 12

  9. Insertion sort algorithm k= 0; while (k < b.length-1) { // Place b[k] in its sorted position in b[0..k] int temp= b[k]; //Save b[k] in temp h= k; // Inv: b[0..k] is sorted except for position b[h], temp <= b[h+1..k] while (h != 0) && b[h-1]>=temp) { b[h]= b[h-1]; h= h-1; } // Note: {b[0..h-1] <= temp <= b[h+1..k]} b[h]= temp; } k= k+1; } Lecture 12

  10. h j k b h h+1 k <= x x >x x ? b Partition • We are going to develop a sorting algorithm called quicksort. An important piece of it is an algorithm called partition, which starts with an array segment that looks like this (x is the initial value in b[h]): (0) • and permutes its elements so that it looks like this: (1) • In rearranging the array in this fashion, the order of the final values of b[h+1..j-1] and b[j+1..k] doesn’t matter Lecture 12

  11. h j k b <= x x >x Partition algorithm B[h..k] contains at least 2 elements. Using the name x for the original value in b[h], permute b[h..k] so that it looks as below and return j publicint partition (int [ ] b, int h, int k ) { int i= h; int j= k; while (i <= j) { if (b[i] <= x) i= i+1; elseif (b[j] > x) j= j-1; else {//Swap b[i] and b[j] int t= b[i]; b[i]= b[j]; b[j]= t;} } // Swap b[h] and b[j] int s= b[i]; b[i]= b[j]; b[j]= s; return j; } Lecture 12

  12. h j k b <= x x >x Recursive Quicksort • Suppose we want to sort b[h..k]. Let’s use algorithm partition to make it look like this, where x is the value initially in b[h]: • What remains to be done in order to sort b[h..k]? Two things: • Sort b[h..j-1] • Sort b[j+1..k] • We present on the next slide a recursive version of quicksort --a version that calls itself. We don’t expect you to fully comprehend it, because recursion, the idea of a method calling itself is new to you. Later, we present a non-recursive version. Lecture 12

  13. h j k b <= x x >x Recursive Quicksort Algorithm // Sort b[h..k] publicstaticvoid quicksort(int[ ] b, int h, int k) { if ( (k-h) == 0) return; // one element in array, so sorted if (h+1-k == 0) { // b[h..k] has exactly 2 elements if (b[h] <= b[k]) return; // Swap b[h] and b[k] int t= b[h]; b[h]= b[k]; b[k]= t; return; } int j= partition(b,h,k); // b[h..k] looks like // Sort b[h..j-1] quicksort(b,h,j-1); // recursive call // Sort b[j+1..k] quicksort(b,j+1,k); // recursive call } Lecture 12

  14. 0 12 start: b 3 6 8 2 7 1 9 4 6 8 7 5 4 b h 0 k 12 j frame h j k After partition b 2 1 3 8 7 6 9 4 6 8 7 5 4 b h 0 k 12 j 2 frame What’s this look like? quicksort ( b, 0, 12); At this point, the call sort(b,h,j-1); has to be executed. Lecture 12

  15. Remember how method calls work • Even with recursion, if we follow the steps for method invocation, everything works out • 1. Draw a frame for the call (place it where?) • 2. Write in the parameters and local variables • 3. Assign arguments to parameters • 4. Execute method body • 5. Erase the frame for the call. Lecture 12

  16. h j k h j k b b 2 1 3 8 7 6 9 4 6 8 7 5 4 1 2 3 8 7 6 9 4 6 8 7 5 4 b h 0 k 12 j 2 b h 0 k 12 j 2 frame0 frame0 b h 0 k 1 j b h 0 k 1 j frame1 frame1 The frames. . . • Just after the second frame has been constructed and the arguments have been assigned to the parameters. We call the first one frame0 and the new one frame1. Executing method body using frame1 Lecture 12

  17. h j k h j k b b 1 2 3 8 7 6 9 4 6 8 7 5 4 1 2 3 4 4 5 6 6 7 7 8 8 9 b h 0 k 12 j 2 b h 0 k 12 j 2 frame0 frame0 Then what? frame1 is now deleted, resulting in The call quicksort(b,h,j-1); is complete. Now, the call quicksort(b,j+1,k); is executed. Thus, another frame will be constructed, to be erased when the call is completed; when the call completes, the situation is: Lecture 12

  18. h j k b <= x x >x Thoughts on Recursion You can see that the recursive calls of quicksort execute correctly by executing the algorithm yourself, using carefully the rules for executing method calls. When trying to understand a method with a recursive call or write a recursive call yourself, don’t go through the exercise of executing it. Instead, do what we did in quicksort. In the situation we see that the array can be sorted simply by sorting the two array segments b[h..j-1] and b[j+1..k]. We can sort these two segments by calling any sorting method we wish, including the one that we are currently writing. If we call the method we are currently writing, we are using a recursive call. In worst case, quicksort make on the order of n2 array comparisons. In the average case, n * log(n). Lecture 12

More Related