Name:
Section:
Question 1:
Consider the following function mentioned in lecture:
#ifndef __QUACK_H__
#define __QUACK_H__ class Quack
...
#endif // __QUACK_H__ |
Quack q1, q2;
q1 = q2;
bool steue( Quack param )
|
void algorithm1( int data[], int n )
{ ... } |
void algorithm2( int data[], int n )
{ ... } |
Question 4:Give tight big-O bounds for algorithm1 and algorithm2, both in terms of n.
algorithm1: O(n), algorithm2: O(n2)
Which algorithm takes fewer steps on a problem of size 1?
algorithm1 takes 150 steps; algorithm2 takes only 2 steps, so algorithm2.
Which algorithm takes fewer steps on a problem of size 100?
algorithm1 takes 10050 steps; algorithm2 takes 29999 steps, so algorithm1.
There exists some crossover point between 1 and 100 where one algorithm becomes worse than the other. Suppose that crossover point is stored in a const int called crossover. Write a function algorithm3 that takes advantage of algorithm1, algorithm2 and crossover to get the best possible performance for any value of n. algorithm3 should have the same interface as algorithm1 and algorithm2.
void algorithm3( int data[], int n )
{
if( n < crossover ) {
algorithm2( data, n );
} else {
algorithm1( data, n );
}
}
Write a recursive function printSeq that takes a positive int parameter and prints the sequence from that int down to 1. For example, printSeq( 3 ) should print out
3 10 5 16 8 4 2 1void printSeq( int n )
BONUS QUESTION:
Give a big-O bound for printSeq. Your bound doesn't
have to be tight -- any upper bound on the running time will do.
For any given starting value of A, let L(A) represent the number of steps it takes to get from A down to 1. Nobody knows what L(A) is! In fact, nobody even knows if L(A) is always finite -- perhaps there is some A for which you never get down to 1! If you can find a big-O bound for L(A), then you will have effectively proven that L(A) is always finite, a result that has evaded some the greatest mathematicians of this century! Good luck.