

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Professor: Gries; Class: Introduction to Computing Using Java; Subject: Computer Science; University: Cornell University; Term: Spring 2009;
Typology: Exams
1 / 2
This page cannot be seen from the preview
Don't miss anything!


1 CS1110 2 April 2009 Sorting: insertion sort, selection sort, quick sort Do exercises on pp. 311-312 to get familiar with concepts and develop skill. Practice in DrJava! Test your methods! Course website contains more prelims and answers, for Prelim 1, 2, and 3 Time spent on A5: min 2 (11 students) average 5. median 5 max 15 15 students took 9-14 hours 26 students took 7-8 hours 2 Comments on A5 Recursion : Make requirements/descriptions less ambiguous, clearer; give more direction. Should be a part where you can create your own recursive functions Need optional problem with more complicated recursive solution would have been an interesting challenge, more recursive functions. Do more of recursive graphics-type problems. They really make us think! Make task 5 easier. I could not finish it. Include a clue that more than one recursive call can be included in a recursive method. I wasted hours because I didn't know this. Clarify clear(), setPanel- Size(). Improve graphic rendering. Explain diff between JFrame and JPanel Liked not having to write test cases! Needed too much help, took too long Add more methods; it did not take long Give us the option to do the recursive methods with loops rather than recursively. 3 Sorting: ? 0 n pre: b sorted 0 n post: b sorted? insertion sort 0 i n inv: b for ( int i= 0; i < n; i= i+1) { } “sorted” means in ascending order 2 4 4 6 6 7 5 0 i 2 4 4 5 6 6 7 0 i Push b[i] down into its sorted position in b[0..i]; Iteration i makes up to i swaps. In worst case, number of swaps needed is 0 + 1 + 2 + 3 + … (n-1) = (n-1)*n / 2. Called an “n-squared”, or n^2 , algorithm. b[0..i-1]: i elements in worst case: Iteration 0: 0 swaps Iteration 1: 1 swap Iteration 2: 2 swaps … 4 ? 0 n pre: b sorted 0 n post: b Add property to invariant: first segment contains smaller values. ≤ b[i..], sorted ≥ b[0..i-1],? 0 i n invariant: b selection sort sorted? 0 i n invariant: b insertion sort for ( int i= 0; i < n; i= i+1) { } 2 4 4 6 6 8 9 9 7 8 9 i n 2 4 4 6 6 7 9 9 8 8 9 i n
int j= index of min of b[i..n-1]; Swap b[j] and b[i]; Also an “n-squared”, or n^2 , algorithm. 5 /** Sort b[h..k] */^ Quicksort public static void qsort (int [] b, int h, int k ) { } if (b[h..k] has fewer than 2 elements) return; j= partition(b, h, k); <= x x >= x h j k post: b x? h k pre: b int j= partition(b, h, k); // b[h..j–1] <= b[j] < b[j+1..k] // Sort b[h..j–1] and b[j+1..k] qsort(b, h, j–1); qsort(b, j+1, k); To sort array of size n. e.g. 2^15 Worst case: n^2 e.g. 2^30 Average case: n log n. e.g. 15 * 2^15 215 = 32768 6 Tony Hoare, in 1968 Quicksort author Tony Hoare in 2007 in Germany Thought of Quicksort in ~1958. Tried to explain it to a colleague, but couldn’t. Few months later: he saw a draft of the definition of the language Algol 58 –later turned into Algol 60. It had recursion. He went and explained Quicksort to his colleague, using recursion, who now understood it.
Viewpoint On teaching programming Reply 7 I don't like how we are forced to visualize things in Dr. Gries' way. … Entire point of programming is to be able to look at things in different ways and come up with different solutions for one problem. Forcing us to think of things in his way and testing us on it has been detrimental to my learn- ing because in my opinion it wastes time and confuses me. This course should focus more on solving problems rather than drawing folders to represent objects.