Fundamentals of Computer Science II (CSC-152 2000S)

Class 21: Algorithm Analysis, Revisited

Back to Lab: Recursion. On to Arrays.

Held Monday, February 28, 2000


Today we revisit the notion of Big-O analysis and analyze the running time of some recursive and non-recursive algorithms.




Notes on Exam 1


Formatting If Statements

Please format nested if statements as follows:

if (test1) {
} // if (test1)
else if (test2) {
} // if (test2)
else if (testn) {
} // if (testn)
else {
} // 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.

X and Y Coordinates

I'll draw a picture to illustrate the meaning of X and Y coordinates. All of you got it wrong.

Moving Pieces

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)) {
      } // 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.

Running Times of Recursive Algorithms

Binary Search


Repeated Multiplication with Iteration

Repeated Multiplication with Recursion


Tuesday, 18 January 2000

Monday, 28 February 2000

Tuesday, 29 February 2000

Back to Lab: Recursion. On to Arrays.

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

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