CSE 326, Winter 1999: Homework 1

Due 1/15/99

For all program or data structure design problems such as the two below you must provide pseudocode (see the manual)and an adequate explanation of the methods. It is often helpful to include small examples demonstrating the methods. Staight pseudocode with no additional documentation is not enough.
  1. (10 points) The classic way to evaluate a polynomial is Horner's Rule. Horner's rule can be stated recursively as follows:

    Let p(x) = a0 + a1*x + a2*x^2 + ... + ak*x^k be a polynomial. Note that * means "times"and ^ means "to the power". To evaluate the polynomial p(x) at c, first let r be the result of evaluating the polynomial a1 + a2*x + a3*x^2 + ... + ak*x^(k-1) at c, then evaluate the final result as a0 + c*r.

  2. (10 points) In this problem you will design a program to find the maximum and minimum in a list of N elements using about 3N/2 comparisons. The obvious way to find the maximum and minimum of a list is to first find the maximum (using the elegant recursive algorithm given in class), then to find the minimum by a variant of the same method. This takes about 2N comparisons. Here is the approach for reducing the number of comparisons to 3N/2. Suppose our list contains c1, c2, ... , cN. For each I between 1 and N/2 compare c(2I-1) and c2I. The larger goes on one list and the smaller on another list. If N is odd then cN goes on both lists. This process takes N/2 comparisons. Now, the maximum can be found on the list of larger elements and the minimum can be found on the list of smaller elements. Each of those lists has size N/2 if N is even and N/2 + 1 if N is odd.