Download presentation
Presentation is loading. Please wait.
Published byJoseph Morgan Modified over 9 years ago
1
CSCE 2100: Computing Foundations 1 Analyzing Iterative Programs Tamara Schneider Summer 2013
2
Running Time of Simple Statements Primitive operations in O(1) – Arithmetic operations (+, %, *, -,...) – Logical operations (&&, ||,...) – Accessing operations (A[i], x->y,...) – Simple assignment – Calls to library functions (scanf, printf,... ) 2
3
Simple For-Loops [1] for(int i=0; i<n; i++) A[i]=0; 3
4
Simple For-Loops [2] Neglect checking for loop condition etc. (constant time) Inner loop: The inner loop is executed n times executed times ⇒ O(n 2 ) for(int i=0; i<n; i++) for(int j=0; j<n; j++) A[i][j]=0; 4
5
Analyzing if statements 5 if( ) else
6
If-else example 6 if(A[1][1] == 0) for(int i=0; i<n; i++) for(int j=0; j<n; j++) A[i][j]=0; else for(int i=0; i<n; i++) A[i][i] = 0; If-part: 2 nested loops executed times each; loop body is O(n) O(n 2 ) total. Else-part: 1 loop executed times; loop body is O(n) total Safe upper bound: O(n 2 )
7
Inner Loop of Selection Sort The loop body takes time O(1) The loop is executed times – The loop starts at – The loop’s condition becomes false for – The loop increment is 1 ⇒ O(n-i-1) 7 for(int j=i+1; j<n; j++) if(A[j] < A[small]) small = j;
8
Selection Sort [1] 8 for(int i=0; i<n-1; i++){ small = i; for(int j=i+1; j<n; j++) if(A[j] < A[small]) small = j; int temp = A[small]; A[small] = A[i]; A[i] = temp; } //for How often is this block executed?
9
Selection Sort [2] 9
10
Complex Loops Some loops do not specify explicitly how many times the loop body is executed – while, do-while, some for-loops Analyze loop to find upper bound on number of iterations May need to prove statement by induction 10
11
Complex Loop Example 11 n = A.size() i = 0; while(x != A[i] && i < n) i++;
12
Programs with Function Calls If function call is non-recursive – Analyze function separately – Add cost of function to block Function calls are a condition of while- or do-while loop – Add cost of function to block 12
13
Example [1] 13 main(){ int a, n; scanf("%d", &n); a = foo(0,n); printf("%d", bar(a,n)); } int bar(int x, int n){ for(int i=1; i<=n; i++) x += i; return x; } int foo(int x, int n){ for(int i=1; i<=n; i++) x += bar(i,n); return x; }
14
Example [2] 1. Check for recursion (direct or indirect) main bar foo 14 main(){ int a, n; scanf("%d", &n); a = foo(0,n); printf("%d", bar(a,n)); } int bar(int x, int n){ for(int i=1; i<=n; i++) x += i; return x; } int foo(int x, int n){ for(int i=1; i<=n; i++) x += bar(i,n); return x; }
15
Example [3] 2. Running time of bar main bar foo O(n) 15 main(){ int a, n; scanf("%d", &n); a = foo(0,n); printf("%d", bar(a,n)); } int bar(int x, int n){ for(int i=1; i<=n; i++) x += i; return x; } int foo(int x, int n){ for(int i=1; i<=n; i++) x += bar(i,n); return x; }
16
Example [4] 3. Running time of foo main bar foo O(n) O(n 2 ) 16 main(){ int a, n; scanf("%d", &n); a = foo(0,n); printf("%d", bar(a,n)); } int bar(int x, int n){ for(int i=1; i<=n; i++) x += i; return x; } int foo(int x, int n){ for(int i=1; i<=n; i++) x += bar(i,n); return x; }
17
Example [5] 4. Running time of main main bar foo O(n) O(n 2 ) 17 main(){ int a, n; scanf("%d", &n); a = foo(0,n); printf("%d", bar(a,n)); } int bar(int x, int n){ for(int i=1; i<=n; i++) x += i; return x; } int foo(int x, int n){ for(int i=1; i<=n; i++) x += bar(i,n); return x; }
18
Summary Primitive operations are considered to have a constant running time. Analyzing simple loops makes use of the number of iterations and the cost of the loop body. To analyze complex loops the “worst case” may need to be assumed. Analyzing conditional statement involves using the “worst case”. Non-recursive functions can be analyzed independently. 18
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.