Computer Science 245
Spring 2009
Homework 5:  Running Time Analysis II
Due Wednesday, March 11th, 3:30 p.m.

  1. Give worst-case Θ() running times for each of the following code fragments, in terms of n.

    1. for (i=0; i<n; i++)
         for (j=0; j < n*n; j++)
            sum++;
         for (j=0; j < n; j++)
            for (k=1; k < n; k = k * 2)
               sum++

    2. for (j = 0; j < 2*n; j++)
      {
         i = n;
         while (i > 1)
         {
            sum++;
            i = i - 2;
         }
         for (i = n; i > 1; i = i / 2)
         {
            sum++;
         }
      }


  2. For each of the following functions:

    • Describe exactly what the function returns
    • Give the worst-case Θ() running time for the function

    1. boolean Mystery(int A[], int B[])
      {
         boolean both = true;
         
         for (int i=0; i < A.length && both; i++)
         {
             int elem = A[i];
             int low = 0;
             int high = B.length-1;
             boolean found = false;
             while (low <= high && !found)
             {
                int mid = (low + high) / 2;
                if (elem == B[mid])
                   found = true;
                else if (elem < B[mid])
                   high = mid - 1;
                else
                   low = mid + 1;
             }
             both = found;
          }
          return both;
      }

    2. int Mystery2(int n)
      {
         int sum = 0;
         for (int i = 1; i <= n; i++)
            for (int j = 1; j <= i; j++)
               sum++;
         int sum2 = 0;
         for (int i = 1; i <= sum; i++)
            sum2 = sum2 + 2;
         for (int i = 1; i <= n; i++)
            sum2 = sum2 - 1;
         return sum2 / n;
      }
         

  3. For each of the following functions:

    • Describe what the function computes.  Be as specific as possible.
    • Give a recurrence relation describing the function.
    • Solve the recurrence relation, using repeated substitution or a recursion tree

    1. int f1(int n)
      {
         if (n > 1)
         {
            return 1 + f1(n/2);
         }
         return 0;
      }

    2. int f2(int a, int b)
      {
         if (a > 0)
         {
            return b + f2(a-1, b)
         }
         else
         {
            return 0;
         }
      }
       
  4. Assume that you have the C array from couting sort (that is, you set C[i] = 0 for all i, then you've incremented C[A[i]] for all values of i from 0 .. n-1, then you've set C[i] =
      C[i] + C[i-1] for all values of i from 1 .. (m-1))

    Write a function numbetween that takes as input this C array, the number of elements m in the C array, and two values low and high, and returns the number of elements in
    the original list whose values are between low and high, inclusive.  You need to be able to handle duplicate entries.  You may assume that low >=0 and low < high, but you can make no other assumptions about the value of high.  Your function should take time (1).  You do not need to turn this in electronically, just a hardcopy will do.  

    int numBetween(int C[], int m, int low, int high)