/* Recursion & Searching */ #include #include #include using namespace std; /* Recursion is a method where solution depends on smaller instances of the same problem. Typically this is implemented as a function calling itself with a different set of parameters. e.g. write a program that uses recursion to countdown from a given parameter. */ /* void countdown(int i){ if ( i != 0 ){ cout << i << endl; countdown( i-1 ); } } int main(int argc, char* argv[]) { if ( argc != 2 ) return 1; countdown( atoi(argv[1])); return 0; } */ /* To keep track of which function to return to, computer uses a call stack. A stack is a first-in last-out (FILO) data structure where a new entry is 'pushed' on top of existing entries. Only the member of the top of the stack can be extracted or 'popped'. Most recursive functions can be written in an iterative form. In C++ by using looping constructs while and for. */ /* ex: write power function using recursion (calculates x to the power y). power(x, y) = 1 if y = 0 x if y = 1 x.power(x, y-1) if y > 1 */ /* int power( int x, int y ) { if ( y == 0 ) return 1; else if ( y == 1 ) return x; else if ( y > 1 ) return x * power( x, y-1 ); } int main(int argc, char* argv[]) { if ( argc != 3) return 1; cout << "result = " << power(atoi(argv[1]),atoi(argv[2])) << endl; return 0; } */ /* ex: write factorial function using recursion. factorial(x) = 1 if x = 1 x.factorial(x-1) if x > 1 */ /* int factorial(int x){ if ( x == 1 ) return 1; else if( x > 1 ) return x * factorial( x - 1 ) ; } int main(int argc, char* argv[]) { if ( argc != 2 ) return 1; cout << argv[1] << "!= " << factorial(atoi(argv[1])) << endl; return 0; } */ /* ex: printing iteratively vs. recursively */ /* class array{ private: int* data; int size; public: array(int n): size(n) { data = new int[size] ; } void set( int x, int v ) { data[x] = v; } int getSize() { return size; } int getItem(int i) { return data[i]; } void printI(); void printR(int); int linearSearch(int); int binarySearch(int); int binarySearchR(int, int, int); }; void array::printI(){ for( int i=0; i hi mid ((hi+lo)/2) if key = item at mid binarySearchR(key,mid+1,hi) if item at mid < key binarySearchR(key,lo,mid-1) if item at mid > key */ /* int array::binarySearchR(int key, int lo, int hi){ if ( lo <= hi ){ int mid = (lo + hi) / 2; if( key == getItem(mid) ) return mid; else if ( getItem(mid) < key ) return binarySearchR(key, mid + 1, hi); else return binarySearchR(key, lo, mid-1); } else return -1; } int main(){ int x ; array A(5); for( int i= 0; i<5; i++ ) A.set(i, i*10); cout << "here is the array: " ; A.printR(0); cout << "looking for item 30..." ; x = A.binarySearchR(30, 0, A.getSize()-1); if ( x == -1 ) cout << "not found!" << endl; else cout << "found at location: " << x << endl; cout << "looking for item 15..." ; x = A.binarySearchR(15, 0, A.getSize()-1); if ( x == -1 ) cout << "not found!" << endl; else cout << "found at location: " << x << endl; return 0; } */ /* Compare linear search with binary search, which one is faster? Is this always the case? Give an example where linear search would run faster than binary. */