CSSE 230
Data Structures and Algorithm Analysis

Written Assignment 2 (62 points)

To Be Turned In

These are to be turned in to the WA2 Drop Box. You can write solutions out by hand and scan them (there is a networked scanner in F-217), or create a file on your computer directly.   After you have submitted, click on the drop box again to verify that your submission was successful.

A single late day may be used or earned for a written assignment: you must turn in all parts early to earn a late day; if you turn in any part late, you will use a late day.

 

Problem numbers [in square brackets] are the numbers from the 3rd edition of the Weiss book.

  1. (2 points) Weiss problem 2.7 [2.6]   (+ with ' ' and " ")
  2. (6 points) Weiss problem 3.13 [3.10]  (legal and illegal references)
  3. (18 points) Weiss problems 4.23-4.25 [4.21–4.23]. (generic max and min).  Assume that all of the items are of the same type T, and that this type implements the Comparable interface. (You can write these out by hand or type them. You don’t have to actually compile and run them, although you certainly may do so, if it increases your confidence that your answers are correct). If you do them in Eclipse, you should either print the code and scan it,  or copy it and paste it into your document
    To make your life a little bit simpler, I am giving you the headers for some methods.

    public static <T extends Comparable<? super T>> T min(T arg1, T arg2)

    public static <T extends Comparable<? super T>> T max(T arg1, T arg2)

    public static <T extends Comparable<? super T>> T min(T[] args)

    public static <T extends Comparable<? super T>> T[] max2(T[] args)

    Creating the Generic array for the return value of the last part is also tricky;  here is a way to do it:

    T[] result = (T[]) Array.newInstance(args[0].getClass(), 2);

  4. (10 points). Use the formal definition of big-Oh (see Weiss section 5.4) to show that N2 + 3N + 8 is O(N2). That is, find appropriate constants N0 and c, and show that they work.
  5. (6 points) Weiss Problem 5.3 [5.3], part a only. (T1(N) + T2(N)). Prove your answer, using the N0 and c idea from the formal definition of big-Oh. [Hint: the given bounds on T1 and T2 tell you something about the existence of an N0 and c for each of those functions: we could call these numbers N01, c1, N02, and c2 ]. Use those constants to come up with appropriate constants for the sum of the functions.)
  6. (8 points) Weiss Problem 5.14 [5.11].   (running time if we go from size 100 to size 500).
  7. (12 points) For each of the following four code fragments, similar to Weiss problem 5.20 [5.15], first give the exact number of times the "sum++" statement executes in terms of n. Then use that to give a Big-Oh running time (by ignoring the low-order terms).
    1. for (int i=0; i < n; i++)
          for (int j=0; j < n * n; j++)
              sum++;
      
    2. for (int i=0; i < n; i++)
          for (int j=i; j < n; j++)
              sum++;
      
    3. for (int i=0; i < n; i++)
          for (int j=0; j < i * n; j++)
              for (int k=0; k < n; k++)
                  sum++;
      
    4. for (int i=n; i >= 1; i = i / 2)
          sum++;
      

For Your Consideration

These problems are for you to think about and convince yourself that you could do them. It would be good practice to actually do them, but you are not required to turn them in.