Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSSE221: Software Dev. Honors Day 22 Announcements Announcements No written part to Homework 8 No written part to Homework 8 You can focus on Simulation.

Similar presentations


Presentation on theme: "CSSE221: Software Dev. Honors Day 22 Announcements Announcements No written part to Homework 8 No written part to Homework 8 You can focus on Simulation."— Presentation transcript:

1 CSSE221: Software Dev. Honors Day 22 Announcements Announcements No written part to Homework 8 No written part to Homework 8 You can focus on Simulation project this week You can focus on Simulation project this week Test 2: programming only Test 2: programming only Animation Quiz. Due at end of break Animation Quiz. Due at end of break Do Angel > Lessons > Other > Simulation Project mid-project assessment today. Do Angel > Lessons > Other > Simulation Project mid-project assessment today.

2 This week: Simulation Project Monday: Monday: Bubble, Selection, and Insertion Sorts Bubble, Selection, and Insertion Sorts File I/O (capsule) File I/O (capsule) Tuesday: Tuesday: Analysis of sorting algorithms Analysis of sorting algorithms Mergesort (capsule) Mergesort (capsule) Thursday: Thursday: Programming mini-exam Programming mini-exam

3 Searching and sorting are ubiquitous In the classic book series The Art of Computer Programming, Donald Knuth devotes a whole volume (about 700 pages) to sorting and searching. In the classic book series The Art of Computer Programming, Donald Knuth devotes a whole volume (about 700 pages) to sorting and searching. Claims that about 70% of all CPU time is spent on these activities. Claims that about 70% of all CPU time is spent on these activities. You need sorting to do fast search You need sorting to do fast search

4 Sorting is a funny word for this concept! Not quite like normal English usage. Not quite like normal English usage. Is there a normal English usage? Is there a normal English usage? From Knuth: From Knuth: He was sort of out of sorts from sorting that sort of data. He was sort of out of sorts from sorting that sort of data. Could “ordering” be a better word? Could “ordering” be a better word? Knuth again: Knuth again: My boss ordered me to order [more memory] so that we could order our data several orders of magnitude faster. My boss ordered me to order [more memory] so that we could order our data several orders of magnitude faster. Actually in Knuth’s (dated) statement, it was “tape drive” instead of “more memory”. Actually in Knuth’s (dated) statement, it was “tape drive” instead of “more memory”.

5 Elementary Sorting Methods Selection sort Selection sort Bubble sort Bubble sort Insertion sort Insertion sort Merge sort Merge sort Quicksort Quicksort Heapsort Heapsort Radix sort Radix sort Shellsort Shellsort Binary tree sort Binary tree sort And lots of others (see Wikipedia) And lots of others (see Wikipedia) Goals: 1. How does each work? 2. Best, worst, average time? 3. Extra space requirements?

6 Intro: Swapping Recall that calling swap(a[i], a[j]) on Recall that calling swap(a[i], a[j]) on swap(int x, int y) { swap(int x, int y) { int temp = x; int temp = x; x = y; x = y; y = temp; y = temp;} doesn’t work! (Why?) Instead call swap(a,i,j) on Instead call swap(a,i,j) on swap(int[]a, int i, int j) { int temp = a[i]; int temp = a[i]; a[i] = a[j]; a[i] = a[j]; a[j] = temp; a[j] = temp;} Extra space?

7 1. Selection Sort Idea: Select largest, then second largest, … Idea: Select largest, then second largest, … http://www.ece.unb.ca/brp/lib/java/selectionsort/ n = a.length for (i = n-1; i>0; i--) { maxPos = 0 // find the largest for (j=1; j<=i; j++){ if (a[j]>a[maxPos]){ maxPos = j } // move it to the end swap(a, i, maxPos) }

8 1. Selection Sort 758492 n = a.length for (i = n-1; i>0; i--) { maxPos = 0 // find the largest for (j=1; j<=i; j++){ if (a[j]>a[maxPos]){ maxPos = j } // move it to the end swap(a, i, maxPos) } After outer loop repeats 3 times: 9 comparisons, but only 3 swaps (9 assignments)452789

9 1. Selection Sort What’s the runtime? What’s the runtime? Best? Best? Worst? Worst? Average? Average? Extra space? Extra space? n = a.length for (i = n-1; i>0; i--) { maxPos = 0 // find the largest for (j=1; j<=i; j++){ if (a[j]>a[maxPos]){ maxPos = j } // move it to the end swap(a, i, maxPos) } 758421

10 2. Bubble Sort Idea: continual swapping gets results (eventually) Idea: continual swapping gets results (eventually) Each adjacent pair of elements is swapped if they are out of order. Each adjacent pair of elements is swapped if they are out of order. n = a.length for (i = n-1; i > 0; i--){ swapped = false for (j = 0; j <= i; j++){ if (a[j] > a[j+1]){ swap(a, j, j+1) swapped = true } if (!swapped) return; }

11 2. Bubble Sort n = a.length for (i = n-1; i > 0; i--){ swapped = false for (j = 0; j <= i; j++){ if (a[j] > a[j+1]){ swap(a, j, j+1) swapped = true } if (!swapped) return; } 758429 452789 After outer loop repeats 3 times (12 comparisons and 21 assignments)

12 2. Bubble Sort What’s the runtime? What’s the runtime? Worst? Worst? Best? Best? Average? Average? Extra space? Extra space? If we tried to act it out, we’d get dizzy from too much swapping! If we tried to act it out, we’d get dizzy from too much swapping! n = a.length for (i = n-1; i > 0; i--){ swapped = false for (j = 0; j <= i; j++){ if (a[j] > a[j+1]){ swap(a, j, j+1) swapped = true } if (!swapped) return; }

13 Interlude: A 5-year old’s understanding of swapping Courtesy of my son Caleb… Courtesy of my son Caleb…

14 3. Insertion Sort Idea: Like sorting files in manila folders Idea: Like sorting files in manila folders http://www.ece.unb.ca/brp/lib/java/selectionsort/ for(i=1; i<a.length; i++){ temp = a[i]; j = i; while (j>0 && temp<a[j-1]){ a[j] = a[j-1]; j--; } a[j] = temp; }

15 3. Insertion Sort for(i=1; i<a.length; i++){ temp = a[i]; j = i; while (j>0 && temp<a[j-1]){ a[j] = a[j-1]; j--; } a[j] = temp; } 457862 After outer loop repeats 3 times (7 comps, 10 assns)758492

16 3. Insertion Sort What is the runtime? What is the runtime? Best? Best? Worst? Worst? Average? Average? Extra space? Extra space? for(i=1; i<a.length; i++){ temp = a[i]; j = i; while (j>0 && temp<a[j-1]) a[j] = a[j-1]; a[j] = temp; }

17 Questions? Then break Then break


Download ppt "CSSE221: Software Dev. Honors Day 22 Announcements Announcements No written part to Homework 8 No written part to Homework 8 You can focus on Simulation."

Similar presentations


Ads by Google