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

for thursday
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

For Thursday

  • Read Weiss, chapter 4, section 3
  • Homework:

– Weiss, chapter 7, exercise 15 – Quicksort homework from ReggieNet

slide-2
SLIDE 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.

slide-3
SLIDE 3

Exam 1

  • Tuesday, October 9
  • Covers material through Quicksort
slide-4
SLIDE 4

Review Questions?

slide-5
SLIDE 5

Programming Assignment 2

  • Any questions?
slide-6
SLIDE 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); } }

slide-7
SLIDE 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]; }

slide-8
SLIDE 8

Performance of Mergesort

slide-9
SLIDE 9

Bottom-up Mergesort

slide-10
SLIDE 10

Quicksort

  • Basic concept:

– We’re going to select a pivot – We’re going to swap items into either smaller

  • r large than the pivot, giving us two portions

(partitions) of the array – We’re going to sort each resulting partition using quicksort

slide-11
SLIDE 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 } }

slide-12
SLIDE 12

Performance of Quicksort

slide-13
SLIDE 13

Improving Quicksort

  • Median of 3
  • Cutoffs
slide-14
SLIDE 14

Bucket Sort

  • Also known as bin sort
  • Radix sort
slide-15
SLIDE 15

External Sorting

  • What’s the issue?
  • What’s the approach?
slide-16
SLIDE 16

K-Way Merging

slide-17
SLIDE 17

Polyphase Merging