for thursday
play

For Thursday Read Weiss, chapter 4, section 3 Homework: Weiss, - PowerPoint PPT Presentation

For Thursday Read Weiss, chapter 4, section 3 Homework: Weiss, chapter 7, exercise 15 Quicksort homework from ReggieNet Extra Credit Opportunity 2005 CS alum Brandon Dewitt VP of Engineering at MoneyDesktop Giving a talk


  1. For Thursday • Read Weiss, chapter 4, section 3 • Homework: – Weiss, chapter 7, exercise 15 – Quicksort homework from ReggieNet

  2. Extra Credit Opportunity • 2005 CS alum Brandon Dewitt • VP of Engineering at MoneyDesktop • Giving a talk Friday at 2:15 in CVA 151 • “Life is beautiful; Unicorns and tales from the Candy Cane Forest” • An extra homework worth of credit if you attend and turn in legible reactions to three things he said during the talk ( just 2-3 sentences each). Typed is appreciated but not required.

  3. Exam 1 • Tuesday, October 9 • Covers material through Quicksort

  4. Review Questions?

  5. Programming Assignment 2 • Any questions?

  6. Mergesort void MergeSort(int arr[], int temp[], int left, int right) { if (left < right) { int center = (left + right)/ 2; // sort left half MergeSort(arr, temp, left, center); // sort right half MergeSort(arr, temp, center+1, right); // merge left and right halves Merge(arr, temp, left, center+1, right); } }

  7. void Merge(int arr[], int temp[], int curLeft, int curRight, int endRight) { int endLeft = curRight – 1; int curTemp = curLeft; int numElems = endRight – curLeft + 1; // Main loop while (curLeft <= endLeft && curRight <= endRight) if ( arr[curLeft] <= arr[curRight]) temp[curTemp++] = arr[curLeft++]; else temp[curTemp++] = arr[curRight++]; while (curLeft <= endLeft) // finish left temp[curTemp++] = arr[curLeft++]; while (curRight <= endRight) // finish right temp[curTemp++] = arr[curRight++]; // copy back to arr for (int i = 0; i < numElems; i++, endRight--) arr[endRight] = temp[endRight]; }

  8. Performance of Mergesort

  9. Bottom-up Mergesort

  10. Quicksort • Basic concept: – We’re going to select a pivot – We’re going to swap items into either smaller or large than the pivot, giving us two portions (partitions) of the array – We’re going to sort each resulting partition using quicksort

  11. void quicksort(int a[], int left, int right) { int i, j, v, temp; if (right - left > 0) { // at least two items in the partition v = a[right]; //v is the pivot i = left - 1; // 1 to the left of the beginning j = right; // 1 to the right of where search starts while (true) { // infinite loop while (a[++i] < v); // pre-increment i until a[i] is >= the pivot while (a[--j] > v); // pre-decrement j until a[j] is <= the pivot if (i >= j) break; //if i and j have crossed -- get out of the loop temp = a[i]; // otherwise, swap a[i] and a[j] a[i] = a[j]; a[j] = temp; } // i and j have crossed, so swap a[i] and the pivot a[right] = a[i]; a[i] = v; // the pivot is now in place at i // now call quicksort on the two partitions quicksort(a, left, i-1); // left partition quicksort(a, i+1, right); // right partition } }

  12. Performance of Quicksort

  13. Improving Quicksort • Median of 3 • Cutoffs

  14. Bucket Sort • Also known as bin sort • Radix sort

  15. External Sorting • What’s the issue? • What’s the approach?

  16. K-Way Merging

  17. Polyphase Merging

Recommend


More recommend