CS 51510 Exam 1 Practice Problems The exam is on Wednesday, September 25. The exam will be over the following sections from the textbook. 1.1, 1.2 2.1, 2.2, 2.3 3.1, 3.2 (up to page 55) 6.1 - 6.4 7.1 - 7.3 The algorithms covered are insertion sort, selection sort, bubble sort, merge sort, quicksort, heapsort. Below are several practice problems that are similar to the kinds of problems that might be on the exam. Problem 1) Trace the steps that insertion sort takes to sort these lists. That is, for each list, show how each iteration of insertion sort will change the list. Use the version of insertion sort from this program, http://cs.pnw.edu/~rlkraft/cs51510/for-class/InsertionSort.java a) [4, 7, 11, 4, 9, 5, 11, 7, 3, 5] b) [–7, 6, 8, 7, 5, 9, 0, 11, 10, 5, 8] Problem 2) Trace the steps that selection sort takes to sort these lists. That is, for each list, show how each iteration of selection sort will change the list. Use the version of selection sort from this program, http://cs.pnw.edu/~rlkraft/cs51510/for-class/SelectionSort.java a) [4, 7, 11, 4, 9, 5, 11, 7, 3, 5] b) [–7, 6, 8, 7, 5, 9, 0, 11, 10, 5, 8] Problem 3) (a) Suppose algorithm A takes five seconds to handle a data set of 1,000 items. If the algorithm A is a linear algorithm, approximately how long will it take to handle a data set of 4,000 items? (b) Suppose algorithm B takes five seconds to handle a data set of 1,000 items. If the algorithm B is a quadratic algorithm, approximately how long will it take to handle a data set of 4,000 items? Problem 4) If you want to find the largest integer in an array of ints, int[] a = { ... }; what is wrong with sorting the array and then getting the last element from the sorted array? sort(a); // sort from smallest to largest int max = a[a.length-1]; Problem 5) Suppose you give an already sorted array as input to both selection sort and insertion sort. Which method will return faster? Briefly explain why. Problem 6) Suppose you need to sort the same list of objects five different ways. Should you use the sort method that takes a list of Comparable objects, or should you use the sort method that takes a Comparator object? Briefly explain why. Problem 7) When you compare numbers, you use the <, >, <=, >=, ==, and != operators. These are all boolean operators that return true or false. When you implement the Comparable or the Comparator interface, you don't write a boolean method, you write a method that returns -1, 0, or 1. Why? Why were Comparable and Comparator designed to return int instead of boolean? Problem 8) A Box has three dimensions, width, length, and height. class Box implements Comparable { public double width, length, height; // implement the compareTo() method } Shipping companies define a measure of boxes called "length plus girth". It is defined to be the sum of the largest dimension plus two times the sum of the two smaller dimensions. length_plus_girth = largest_dim + 2*(smallest_dim + middle_dim) a) Write the compareTo method for the Box class so that is compares boxes by their "length plus girth". b) Complete the implementation of the CompareBoxes class so that the comparator compares two boxes by their "length plus girth". class CompareBoxes implements Comparator { // implement the compare() method } Problem 9) An Interval is two integers where the first integer must be less than or equal to the second one. class Interval implements Comparable { private int a, b; public Interval(int a, int b) { if (b < a) throw new IllegalArgumentException(); this.a = a; this.b = b; } // implement the compareTo() method } a) Write the definition of the compareTo() method so that two Interval objects are compared by their length. b) Write the definition of the compare() method so that the Comparator compares two Interval objects by where they begin on the number line. An Interval is less than any other Interval that begins after it. If two Interval objects begin at the same number, use their other number as the tie breaker (the shorter Interval is less than the longer Interval). class CompareIntervals implements Comparator { // implement the compare() method } Problem 10) Suppose that we have defined a method, public static void quadFunction(int n){ ... } that has quadratic time complexity O(n^2). Determine the time complexity for each of the following functions that use quadFunction(). For each function, give a brief explanation of your reasoning. public static void function1(int n) { for (int i = 0; i < n; ++i) { quadFunction( n ); } } public static void function2(int n) { quadFunction( pow(n,2) ); } public static void function3(int n) { for (int i = 1; i < n; i*=2) { quadFunction( n ); } } public static void function4(int n) { for (int i = 0; i*i < n; ++i) { quadFunction( n ); } } public static void function5(int n, int k) { for (int i = 0; i < k; ++i) { quadFunction( n ); } } public static void function6(int n) { function5( n, pow(n,3) ); } public static void function7(int n, int k) { quadFunction( n ); quadFunction( k ); } public static void function8(int n) { function7( n, pow(n,3) ); } public static void function9(int n, int k) { if (k <= 0) return; else { quadFunction( n ); function9( n, k-1 ); } } public static void function10(int n) { for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) quadFunction( n ); } Problem 11) Suppose we have a 2-dimensional, n-by-n array, as in int[][] A = new int[n][n]; and suppose we need to find the smallest element in the array. One student claims that finding the smallest element in the array will need O(n^2) time but another student says that in class they learned that finding a minimum was a linear, not a quadratic, time algorithm, so finding the smallest element in the array will need O(n) time. Which student is right? Explain why. Problem 12) Below are two methods that each compute the value of x^n for any double x and for positive integers n that are of the form 2^k with 0 <= k, i.e., n must be a power of 2. One of these methods implements an effective divide and conquer strategy, the other does not. Which one uses divide and conquer effectively? Explain why and explain what is wrong with the other one. Also, analyze each function for how many multiplication operations are needed, as a function of n, in the calculation of x^n. public static double powerA(double x, int n) // n must be a power of 2 { if (n == 1) return x; else { return powerA(x, n/2) * powerA(x, n/2); } } public static double powerB(double x, int n) // n must be a power of 2 { if (n == 1) return x; else { double z = powerB(x, n/2); return z * z; } } Problem 13) Does the following array represent a max-heap? If so, draw a picture of the heap. If not, explain what is wrong. 11 8 9 4 5 7 1 3 0 2 Problem 14) Show the result of executing Max-Heapify on the following array. Show your result as both an array and as a tree. 5 9 8 4 6 7 1 3 0 2 Problem 15) Suppose we define the following array, final int[] a = {4, 3, 5, 4, 2, 4, 7, 4, 1, 2}; and then call the partition() method on this array, final int p = QuickSort.partition(a, 0, a.length-1); What is the value of p and what is the value of the array a after the partition() method returns? Use the version of partition() from this program, http://cs.pnw.edu/~rlkraft/cs51510/for-class/QuickSort.java