CMSI 281
Homework #5

Read [Gray], Chapters 8 and 9. Consider the "Checkpoint" questions as part of your reading assignment: answer them but don't turn in your answers.You are encouraged to do all of these because many of the quiz and final exam questions will look surprisingly similar to them.

Use your CVS repository (or an SVN repository if you prefer) while working on this assignment.

Provide professionally crafted solutions to the following:

  1. Write a Java class called StupidMathUtils with a private constructor and four (possibly stupid and inefficient) recursive algorithms for:
    1. The function a, defined like this (x and y are BigIntegers):
          a(0, y) = y + 1             if y ≥ 0
          a(x, 0) = a(x-1, 1)         if x > 0
          a(x, y) = a(x-1, a(x, y-1)) if x > 0, y > 0
      
      (Throw an exception if x or y is null or less than zero — you ought to know which one to throw.)
    2. A function that, when given an integer, returns the string containing the binary representation of the integer. For example, for 43, return "101011", and for -9 return "-1001".
    3. A function that takes in an integer n and returns the largest integer less than or equal to log3n.
    4. A method to compute xn, where x is a BigInteger and n is an integer. Your algorithm should take about log n "steps", not n-1 steps.
    Remember you have to write these methods recursively, even though that may not the best approach.
  2. Write a unit test for the above utility class.
  3. What value should be returned from calling StupidMathUtils.a(4, 2)? (SHOW YOUR WORK.)
  4. Consider the function C defined recursively as follows:
        C(n, k) = 1, if k = 0 or k = n
                = C(n-1, k-1) + C(n-1,k) if 0 < k < n
                = undefined, otherwise
    
    
    It can be shown that C can also be defined non-recursively:
        C(n,k) = n! / (k!(n-k)!)
    
    Give the worst-case complexity of (a) the naïve recursive, (b) the non-recursive algorithms for computing C(n, k).
  5. Consider the following method, which displays all possible strings of a given length from a given alphabet to standard output.
    public static void showStrings(char[] alphabet, int length) {
        show("", alphabet, length);
    }
    
    For example, the call showStrings(new char[]{'a', 'b', 'c'}, 2) displays:
        aa
        ab
        ac
        ba
        bb
        bc
        ca
        cb
        cc
    
    Write the helper method show recursively. Since this method's job is to print, you won't be writing a unit test for it. Later in your career, you will see how to generalize this method so it writes to a stream. Then a tester can pull from the stream and perform tests. For now, just be happy inspecting the code for correctness.
  6. Draw detailed pictures of the progression of the following algorithms as they sort the array 65 9 17 22 20 8 2 6 1 80 44.
    1. Selection Sort
    2. Gnome Sort
    3. Quick Sort (using first element as pivot)
    4. Quick Sort (using median of three as pivot)
    5. Merge Sort

Organize your work in your CVS (or SVN) repository with the following structure:

homework
  cmsi281
    src
      main
        docs
          hw5.tex
          <diagram source files>, if any
          <images>, if any
        java
          edu
            lmu
              cs
                math
                  StupidMathUtils.java
                test
                  StringGenerator.java
      test
        java
          edu
            lmu
              cs
                math
                  StupidMathUtilsTest.java

Your LaTeX file will contain solutions to each problem, numbered, and in order. You may embed source code in the document, or give the answer as "See <filename>" and submit the sources for these answers at the end of the document. Generate a pdf from the LaTeX file and hand in a printed copy. Only turn in printed copies of your own files; do not waste trees by printing the sources for the code I gave you.

Notes