CSE 1342 Programming Concepts

13
CSE 1342 Programming Concepts Algorithmic Analysis Using Big-O Part 2

description

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. - PowerPoint PPT Presentation

Transcript of CSE 1342 Programming Concepts

Page 1: CSE 1342 Programming Concepts

CSE 1342 Programming Concepts

Algorithmic Analysis Using Big-OPart 2

Page 2: CSE 1342 Programming Concepts

Computing the Run Time of Loop Statements Without Function Calls

Page 3: CSE 1342 Programming Concepts

Computing the Run Time of an if-Statement Without Function Calls

Page 4: CSE 1342 Programming Concepts

Computing the Run Time of a Block Without Function Calls

Page 5: CSE 1342 Programming Concepts

Program Illustrating Non-Recursive Function Calls

Page 6: CSE 1342 Programming Concepts

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)

Page 7: CSE 1342 Programming Concepts

Selection Sort

Page 8: CSE 1342 Programming Concepts

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)

Page 9: CSE 1342 Programming Concepts

Recursive Binary Searchint 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

}

Page 10: CSE 1342 Programming Concepts

Recursive Binary Search Analysis

int a[100], value = 20; //assume array has been initialized

recSearch(a, 0, 99, value); //initial call from main( )

INITIAL CALL FOR a[0 TO 99]

2nd CALL FOR a[0 TO 48]

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 or

Binary Search is O(log2 n) or O(log n)Notice, there is only 1 call made at each level

Page 11: CSE 1342 Programming Concepts

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) }

Page 12: CSE 1342 Programming Concepts

Towers of Hanoi Analysis

towers(3, 1, 3, 2); //initial call from main( )

INITIAL CALL n = 3; return to main

2nd CALL n = 2; return to 1

5th CALLn = 2; return to 2

3rd CALLn = 1; return to 1

4th CALLn = 1; return to 2

6th CALLn = 1; return to 1

7th CALLn = 1; return to 2

and

and and

Towers is O(en)Notice, every call at each level is made

Page 13: CSE 1342 Programming Concepts

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.