1 / 17

CSSE221: Software Dev. Honors Day 22

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.

Download Presentation

CSSE221: Software Dev. Honors Day 22

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. 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.

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

  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. • Claims that about 70% of all CPU time is spent on these activities. • You need sorting to do fast search

  4. 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”.

  5. 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?

  6. 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?

  7. 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

  8. 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)

  9. 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

  10. 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

  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; } After outer loop repeats 3 times (12 comparisons and 21 assignments)

  12. 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

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

  14. 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

  15. 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)

  16. 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

  17. Questions? • Then break

More Related