






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







The material on the order notation at the end the lecture is covered in Ford&Topp, pages 128-139.
What follows is a quick review of counting and the order notation.
Suppose foo is an array of n doubles, initialized with a sequence of values.
double sum = 0; for ( int i=0; i<n; ++i ) sum += foo[i];
The following discussion emphasizes intuition. That’s all we care about in Data Struc- tures. For more details and more technical depth, see any textbook on data structures and algorithms.
Algorithm A is order f (n) — denoted O(f (n)) — if constants k and n 0 exist such that A requires no more than k × f (n) time units (operations) to solve a problem of size n ≥ n 0.
In each case give an order notation estimate as a function of n which here notes the
int count=0; for ( int i=0; i<n; ++i ) for ( int j=0; j<n; ++j ) ++count;
int count=0; for ( int i=0; i<n; ++i ) ++count;
for ( int j=0; j<n; ++j ) ++count;
int count=0; for ( int i=0; i<n; ++i ) for ( int j=i; j<n; ++j ) ++count;
Solutions to these will be posted on-line:
n! =
n · (n − 1)! n > 0 1 · n == 0
np^ =
n · np−^1 p > 0 1 · p == 0
C++, like other modern programming languages, allows functions to call themselves. This gives a direct method of implementing recursive functions.
int fact( int n ) { if ( n == 0 ) return 1; else { int result = fact( n-1 ); return n * result; } }
int ifact( int n ) { int result = 1; for ( int i=1; i<=n; ++i ) result = result * i; return result; }
Write an iterative version of intpow.
Here is an outline of five steps I find useful in writing and debugging recursive functions:
The following example is important for thinking about the mechanisms of recursion.
void print_vec( vector
void print_vec( vector
int main() { vector
v[0] <= v[1] <= v[2] <= ...
On Thursday we will proceed to C++ classes.