CSC153 2004S, Class 54: Discussion of Exam 3 and other Stuff Admin: * Exam 3 graded and Lakers lost! Yay! * I am so pleased to have Joe and Chase and Erik and Saul here today. * Hmmm ... I am returning harshly graded exams today and you are evaluating me tomorrow. Am I an idiot or what? * Option for Monday: Indian. Chosen. * Survey distributed tomorrow. To be discussed at lunch on Monday. Overview: * Discussion of Exam 3 * Global lessons for CSC153 Problem 1: * Fix really ugly code * Lessons: * Don't update your loop variable in the body of a for loop * You have a responsiblity for both comments and code * Worry about special cases Problem 2: Dynamic Arrays * You need to document! * What should size()/length return? * The length of the "internal" array (at least 3). * The index of the "last" (greatest) filled position. * What should you do if set() uses a position greater than the length of the internal array? * Bad strategy: Set the size to pos+1 * You are likely to want to set something immediately past that * Example: for (i = 0; i < 100; i++) stuff.set(i, i); * Running time O(N^2): 11 + 12 + 13 + 14 + 15 + ... + 100 = close to N(N+1)/2 * Solution: Increase the size more dramatically: * Repeatedly double the size until it's big enough * Repeatedly increment the size by 1/2 until it's big enough int newSize = this.size; while (newSize <= pos) newSize = newSize * factor; this.resize(newSize); * What should get return on an index greater than "length"? * Throw an exception! * Design idea: There are no invalid positive indices! * Documentation for get /** * Get the value most recently put in position pos with set(pos, v). * If there is no such value, returns null. */ public Object get(int pos) 3. Orders and other similar stuff: * Biggest concern: No documentation of mayPrecede() 4. Selection sort for (int i = 0; i < stuff.length(); i++) { for (int j = i+1, j < stuff.length(); i++) { if (compare.mayPrecede(stuff.get(i), stuff.get(j))) swap(stuff, i, j); } // inner for } // outer for (1) The key idea of selection sort is lost in this code! for i = 1 to length put the smallest thing in postions i .. length into position i (2) Putting the smallest thing at position i requires a lot of swaps. Swaps are usually comparatively expensive. Selection sort can be done with one swap per loop, so do it. (3) Calling length() at every iteration is inefficient, given that you know the length won't change. int len = stuff.length(); for (int i = 0; i < len; i++) { swap(stuff, i, indexOfSmallest(stuff, i,len-1)); } public static int indexOfSmallest(Order order, DynamicArray stuff, int start, int end) { int guess = start; for (int j = start+1; j <= end; j++) if (order.mayPrecede(stuff.get(j), stuff.get(guess))) guess = j; return guess; } Testing! * Look! It sorts arrays of three things! IDs should not be integers! ---- Stupid side note: Online grading tested on YOUR CLASS! ---- Conclusions from the class: * Hurt the user [No, this is not a real conclusion] * Have a nice day!