CSSE 230
Data Structures and Algorithm Analysis

Written Assignment 2

To Be Turned In

These are to be turned in as hard copy. You can write solutions out by hand, or write them on your computer and print them.

Papers must not have ragged, spiral-notebook edges. Multiple page solutions must be stapled. Solutions not following these guidelines will be returned ungraded. I regret this rigidity, but it is necessary given the number of students in CSSE 230 this term.

Late days may not be used for written assignments.

  1. (18 points) Weiss problems 4.21–4.23 (page 157). Assume that all of the items are of the same type T, and that this type IS-A Comparable. (You can write these out by hand or type them. You don’t have to actually compile and run them, though you certainly may.)
  2. (30 points) Weiss problems 4.29 and 4.30. This isn’t exactly a “written problem”, but it comes from the book and requires very little code (if done correctly), so I’m including it in this written assignment.

    Starting code is in your individual SVN repository. (See the first programming assignment for your repository URL.) Use Subclipse to check out the project CountMatches. It includes JUnit tests for all of the parts. I’ve put TODO items in the code indicating where you should add your solution and giving some additional tips for each part of the problem.

    When you finish the TODO items and pass the tests, just commit your solution back to your repository. You don’t need to turn in this part on paper.

    Eclipse provides a view that will list all the outstanding TODO and FIXME comments in a project. From the Eclipse Menus choose Window → Show View → Tasks. The new view should appear at the bottom. You can double click on an item in the task list to jump to the corresponding comment in the code. (Eclipse 3.4.0 had a bug where this view wouldn’t update immediately. If you don’t see any TODO items for the CountMatches project, choose Project → Clean... to regenerate the list for CountMatches.)

  3. (10 points).  Use the formal definition of big-Oh (see Weiss §5.4) to show that N2 + 3N + 8 is O(N2).  That is, find appropriate constants N0 and c, and show that they work.
  4. (6 points) Weiss Problem 5.3, part a only.  Explain 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 existance of an N0 and c for each function, an N01, c1, N02, and c2 if you will. Use those constants to come up with appropriate constants for your answer.)
  5. (12 points) For each of the following four code fragments, taken from Weiss problem 5.15, give a Big-Oh analysis of the running time. By “analysis” I mean that you should give the Big-Oh answer and explain how you arrived at it.
    1. for (int i=0; i < n; i++)
          sum++;
      
    2. for (int i=0; i < n; i += 2)
          sum++;
      
    3. for (int i=0; i < n; i++)
          for (int j=0; j < n; j++)
              sum++;
      
    4. for (int i=0; i < n; i++)
          sum++;
      for (int j=0; j < n; j++)
          sum++;
      
  6. (8 points) Weiss Problem 5.11.

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.