













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Pompei; Class: Programming & Problem Solving II; Subject: Computer Engr & Computer Sci; University: California State University - Long Beach; Term: Unknown 1989;
Typology: Study notes
1 / 21
This page cannot be seen from the preview
Don't miss anything!














Defn: An algorithm is a list of instructions to accomplish a given task. An algorithm has: 0 or more inputs 1 or more results unambiguous instructions a finite number of steps in which it terminates Defn: An algorithm is a finite sequence of instructions, each of which has a clear meaning and can be performed with a finite amount of effort in a finite length of time using a finite amount of memory.
Example: Design an algorithm that recognizes palindromes Defn: A palindrome is a string that reads the same forward and backward. Algorithm 1:
C++ Code for the standard (iterative) definition int factorial (int n) { int ans = 1; for (int i = 2; i <= n; i++) ans *= i; return ans; } C++ Code for the recursive definition int factorial (int n) { if (n <= 0) // or if (n <= 1) return 1; return n * factorial (n - 1); } Both the iterative and recursive functions work correctly, but how do they compare in terms of performance efficiency?
The context or execution environment of a function contains all of the following (and probably more): local variables, parameters, the address of the instruction the function is currently executing and the return address of the instruction to execute upon termination of the currently executing function. When a call is made to a new function, we say that the context or execution environment of the current function has switched to that of the new function. When a context or execution environment is switched , the previous context or execution environment must be saved so that it can be reinstated upon return from the function call.
Context switching in Recursive Calls int main() { int X, Y; float Z; //… Z = funct1(X, Y); //… return 0; } float funct (int X1, int X2) { int Y1, Y2; float R1, R2; //… if (SomeCond) R1 = funct1(Y1, Y2); //… return R2; } float funct (int X1, int X2) { int Y1, Y2; float R1, R2; //… if (SomeCond) R1 = funct1(Y1, Y2); //… return R2; }
Example 2: Fibonacci Numbers (or Rabbits from pp. 74-77 of the Textbook) Defn: The Fibonacci Numbers can be recursively defined by the equations: F(n) = F(n-1) + F(n-2) F(1) = 1; F(0) = 0 int Fibonacci( int n) // // Purpose: Computes a term in the Fibonacci Sequence // Precondition: n is a non-negative integer. // Postcondition: Returns the nth Fibonacci Number { if (n < 1) return 0; else if (n == 1) return 1; else return Fibonacci(n-1) + Fibonacci(n-2); }
Recursive Algorithm:
C++ Code for Permutations #include <iostream.h> const SIZE = 4; void permute(char data[], int pos); int main() { char array[SIZE] = {'A', 'B', 'C', 'D'}; int pos = 0; permute(array, pos); return 0; } // Note: pos is set to 0 the first time this function is invoked void permute(char data[], int pos) { char new_data[SIZE]; char hold; // copy data to new_data for (int i = 0; i < SIZE; i++) new_data[i] = data[i]; if (pos >= SIZE - 1) { // this is the base case, so print for (int i = 0; i < SIZE; i++) cout << data[i]; cout << endl; } else { for (int next = pos; next < SIZE; next++) { // pick a letter and put it in the // front part hold = new_data[next]; new_data[next] = new_data[pos]; new_data[pos] = hold; // recursive call permute(new_data, pos + 1); } } }
Example 4: Binary Search Goal: Search a sorted array of data for a particular item. Idea: A binary search takes advantage of the fact that the array is already sorted. Algorithm: Given a sorted array, and an item you are searching for, examine the middle element of the array. If the middle element
Binary Search Notes: Notice, the array we are searching is effectively divided into two parts at each level of the recursion. The base cases for the recursion occur when the item is found or it is determined that the item is not in the array. If the item you want is not found, notify the caller of the function of this fact. Remember a binary search will not work with an unsorted array. Our approach will return the index in the array to the position of the item we are searching for.
Goal : Develop a method for comparing the performance (run time) of different algorithms. Problem: Stating the running time of an algorithm is difficult. There are just too many variables. For example: the input to the program the machine it is running on the programming language used Solution: Performance approximation. Defn: Performance approximation is a generalization method of stating how an algorithm (or program) will perform as the size of the problem grows.
O-Notation O-Notation is used to describe the approximate performance of an algorithm (or program). Defn: An algorithm is said to be O(T(n)) if the time it takes to run is <= c * T(n) where T(n) is a function, n is the size of the problem, c is a constant. O(T(n)) is pronounced “Big-O of T(n)”.
O-notation Analysis of Algorithms Case 1: Linear Search of an Array n = the size of the array Search time: n/2 in the average case n in the worst case O-notation: O(n) Case 2: Binary Search of an Array n = the size of the array Array Size Comparisons 1 1 2 2 3 2 4 3 7 3 8 4 15 4 16 5 n log 2 n + 1 Search time: log 2 n + 1 in the worst case O-notation: O(log 2 n)
Algorithm Running Times ”Faster” Algorithms log 2 n
n n log 2 n n 2 n 3 (1.5) n 2 n 3 n n! n n “Slower” Algorithms