170 likes | 275 Views
CSSE221: Software Dev. Honors Day 22. Announcements No written part to Homework 8 You can focus on Simulation project this week Test 2: programming only Animation Quiz. Due at end of break Do Angel > Lessons > Other > Simulation Project mid-project assessment today.
E N D
CSSE221: Software Dev. Honors Day 22 • Announcements • No written part to Homework 8 • You can focus on Simulation project this week • Test 2: programming only • Animation Quiz. Due at end of break • Do Angel > Lessons > Other > Simulation Project mid-project assessment today.
This week: Simulation Project • Monday: • Bubble, Selection, and Insertion Sorts • File I/O (capsule) • Tuesday: • Analysis of sorting algorithms • Mergesort (capsule) • Thursday: • Programming mini-exam
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. • Claims that about 70% of all CPU time is spent on these activities. • You need sorting to do fast search
Sorting is a funny word for this concept! • Not quite like normal English usage. • Is there a normal English usage? • From Knuth: • He was sort of out of sorts from sorting that sort of data. • Could “ordering” be a better word? • Knuth again: • 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”.
Selection sort Bubble sort Insertion sort Merge sort Quicksort Heapsort Radix sort Shellsort Binary tree sort And lots of others (see Wikipedia) Elementary Sorting Methods Goals: 1. How does each work? 2. Best, worst, average time? 3. Extra space requirements?
Intro: Swapping • Recall that calling swap(a[i], a[j]) on swap(int x, int y) { int temp = x; x = y; y = temp; } doesn’t work! (Why?) • Instead call swap(a,i,j) on swap(int[]a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } Extra space?
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) } 1. Selection Sort
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) } 1. Selection Sort After outer loop repeats 3 times: 9 comparisons, but only 3 swaps (9 assignments)
What’s the runtime? Best? Worst? Average? 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) } 1. Selection Sort
Idea: continual swapping gets results (eventually) 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; } 2. Bubble Sort
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; } After outer loop repeats 3 times (12 comparisons and 21 assignments)
What’s the runtime? Worst? Best? Average? Extra space? 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; } 2. Bubble Sort
Interlude: A 5-year old’s understanding of swapping • Courtesy of my son Caleb…
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; } 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; } 3. Insertion Sort After outer loop repeats 3 times (7 comps, 10 assns)
What is the runtime? Best? Worst? Average? 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; } 3. Insertion Sort
Questions? • Then break