130 likes | 220 Views
CSE 1342 Programming Concepts. Algorithmic Analysis Using Big-O Part 2. Computing the Run Time of Loop Statements Without Function Calls. Computing the Run Time of an if-Statement Without Function Calls. Computing the Run Time of a Block Without Function Calls.
E N D
CSE 1342 Programming Concepts Algorithmic Analysis Using Big-O Part 2
Computing the Run Time of Loop Statements Without Function Calls
Computing the Run Time of an if-Statement Without Function Calls
Analyzing Programs with Non-Recursive Function Calls • Evaluate the running times for functions that do not call other functions. • bar - O(n) + O(1) = O(n) • Evaluate the running times of functions that only call functions already evaluated. • foo - O(n * n) + O(1) = O(n2) + O(1) = O(n2) • Proceed in this fashion until all functions have been evaluated. • main = O(n2 ) + O(n) + O(1) = O(n2)
Selection Sort Analysis • The selection sort involves a nested loop that is driven be the control variable i in the outer loop. Each time a pass is made through the outer loop, the number of times through the is 1 less than the previous time. • Outer loop passes = f(n - 1) • Inner loop passes = f((n-1)+(n-2)+(n-3)+ … +1 • To sum the inner loop (the values from 1 to n), use the formula n(n-1)/2, where n = n-1 • (n-1)((n-1)-1)/2 = ((n-1)2-n-1)/2 = (n2-3n)/2 =O(n2)
Recursive Binary Search int recSearch(int a[], int lb, int ub, int value) { //Recursive binary search routine int half; if(lb > ub) return -1; //value is not in the array half = (lb+ub) / 2; if(a[half] == value) //value is in the array return half; //return value's location else if(a[half] > value) return recSearch(a, lb, half-1, value); //search lower half of array else return recSearch(a, half+1, ub, value); //search upper half of array }
Recursive Binary Search Analysis INITIAL CALL FOR a[0 TO 99] 2nd CALL FOR a[0 TO 48] int a[100], value = 20; //assume array has been initialized recSearch(a, 0, 99, value); //initial call from main( ) or 2nd CALL FOR a[50 TO 99] 3rd CALL FOR a[0 TO 23] 3rd CALL FOR a[25 TO 48] 3rd CALL FOR a[50 TO 73] 3rd CALL FOR a[75 TO 99] or or Binary Search is O(log2 n) or O(log n) Notice, there is only 1 call made at each level
Towers of Hanoi void towers(int disks, int start, int end, int temp) { if (disks == 1) { cout << start << “ TO ” << end << endl; return; } // move disks – 1 disks from start to temp towers (disks – 1, start, temp, end); (1) // move last disk from start to end cout << start << “” << end << endl; // move disks – 1 disks from temp to end towers (disks – 1, temp, end, start); (2) }
Towers of Hanoi Analysis INITIAL CALL n = 3; return to main 2nd CALL n = 2; return to 1 towers(3, 1, 3, 2); //initial call from main( ) and 5th CALL n = 2; return to 2 3rd CALL n = 1; return to 1 4th CALL n = 1; return to 2 6th CALL n = 1; return to 1 7th CALL n = 1; return to 2 and and Towers is O(en) Notice, every call at each level is made
Towers of Hanoi Analysis • In terms of speed efficiency the Towers algorithm is O(2n) - exponential. • To illustrate, by adding another disk to the original call the number of recursive calls doubles. Add another disk and the number of calls doubles again. • In terms of memory usage the Towers algorithm is O(n) because the system stack will be at most n elements high.