[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Project] [Outlines] [Labs] [Assignments] [Quizzes] [Exams] [Examples] [EIJ] [JPDS] [Tutorial] [API]
Back to Lab: Recursion. On to Arrays.
Held Monday, February 28, 2000
Overview
Today we revisit the notion of Big-O analysis and analyze the running time of some recursive and non-recursive algorithms.
Notes
Contents
Summary
Please format nested if
statements as follows:
if (test_{1}) { statements_{1} } // if (test_{1}) else if (test_{2}) { statements_{2} } // if (test_{2}) ... else if (test_{n}) { statements_{n} } // if (test_{n}) else { default } // none of the above
That is, put the else
and subsequent if
on the same line and use the same level of nesting. This makes
it easier to fit lots of conditions in a reasonable amount of
space.
I'll draw a picture to illustrate the meaning of X and Y coordinates. All of you got it wrong.
A number of you had trouble finding statistically valid ways to move pieces. In particular, the typical solution was something like this:
for horizOffset = -1 to 1 for vertOffset = -1 to 1 if (horizOffset != 0) || (vertOffset != 0) { int newx = this.x + horizOffset; int newy = this.y + vertOffset; if (!board.occupied(newx,newy)) { this.set(newx,newy); board.updatedLocation(this); return; } // if the space is free } // if at least one offset is nonzero
Unfortunately, this type solution is more likely to move the piece to particular locations than others. This particular solution will tend to move pieces to the left and downwards.
A second solution was to repeatedly choose a random number between 1 and 8 and check if the ``corresponding space'' was free. You keep choosing and checking until a free space is found. This is inefficient. It will also run forever if there are no free spaces around.
The best solution is to count the number of available spaces and choose a random number between 1 and that number. You can then use the corresponding space.
/** * An illustration of different mechanisms for computing x^n. * * @author Samuel A. Rebelsky * @version 1.0 of October 1999 */ public class Exponentiation { // +---------+------------------------------------------------- // | Methods | // +---------+ /** * Compute x^n by multiplying x*x*...*x n times. Uses * iteration as the mechanism for repetition. */ public static double expA(double x, int n) { // Handle negative exponents. x^(-n) = 1/(x^n) if (n < 0) { return 1/expA(x,-n); } // Prepare to compute the product. double product = 1; // Multiply by each x. (Note that 1*x*x*...*x = x*x*...*x.) for (int i = 0; i < n; ++i) { product = product * x; } // for // That's it, we're done. return product; } // expA(double,int) /** * Compute x^n by multiplying x*x*...*x n times. Uses * recursion as the mechanism for repetition. */ public static double expB(double x, int n) { // Handle negative exponents. x^(-n) = 1/(x^n) if (n < 0) { return 1/expB(x,-n); } // if (n < 0) // Base case: x^0 is 1. if (n == 0) { return 1; } // if (n == 0) // Recursive case: x*x*...*x = x*(x*...*x) // That is: x^n = x*(x^(n-1)) else { return x * expB(x, n-1); } // Recursive case, if (n > 0) } // expB(double,int) /** * Compute x^n by a recursive divide-and-conquer algorithm. */ public static double expC(double x, int n) { // Handle negative exponents. x^(-n) = 1/(x^n) if (n < 0) { return 1/expB(x,-n); } // if (n < 0) // Base case: x^0 is 1. if (n == 0) { return 1; } // Base case: n == 0 // Recursive case (when n is odd) // x*x*...*x = x*(x*...*x) // That is: x^n = x*(x^(n-1)) else if (n % 2 == 1) { return x * expC(x,n-1); } // Recursive case (when n is odd) // Recursive case (when n is even) // Let n be 2k // x^n = x^(2k) = x^k * x^k else { int k = n/2; double tmp = expC(x,k); return tmp*tmp; } // Recursive case (when n is even) } // expC } // class Exponentiation
expA
, is perhaps the easiest to
analyze.
expB
, is somewhat more difficult
to analyze because of the recursion.
Tuesday, 18 January 2000
Monday, 28 February 2000
Tuesday, 29 February 2000
Back to Lab: Recursion. On to Arrays.
[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Project] [Outlines] [Labs] [Assignments] [Quizzes] [Exams] [Examples] [EIJ] [JPDS] [Tutorial] [API]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/2000S/Outlines/outline.21.html
Source text last modified Tue Feb 29 09:01:32 2000.
This page generated on Tue Feb 29 09:14:41 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu