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

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


Notes on Exam 1

General

Formatting If Statements

Please format nested if statements as follows:

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

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

Running Times of Recursive Algorithms

Binary Search

Exponentiation

Repeated Multiplication with Iteration

Repeated Multiplication with Recursion


History

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 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