Professional Documents
Culture Documents
06 Complexity and Asymtotic Analysis
06 Complexity and Asymtotic Analysis
06 Complexity and Asymtotic Analysis
1
Emprical Analysis
Data Analysis
How many steps?
Standard analysis techniques
• Standard analysis techniques
– Constant time statements
– Analyzing Loops
• For Loop
• Do-While Loop
• Nested Loops
– Conditional Statements
– Sequence of Statements
Constant Time Statements
Simplest case: O(1) time statements
• Assignment statements of simple data types
int x = y;
• Arithmetic operations
x = 5 * y + 4 - z;
count = count + 1;
• Array referencing / assignment
A[j] = 5;
• Most conditional tests
if (x < 12) ...
Constant Time Statements
Example: Simple If-Statement
Times
if (n < 0) 1
absval = -n 1
else
absval = n; 1
currentMax A[0] 2
for (i =1; i<n; i++) 1+n+
n-1
if A[i] currentMax then 2(n
1)
currentMax A[i] 2(n
1)
Analyzing Loops
Example: Simple Loop
Times
i = 1; 1
sum = 0; 1
while (i <= n) { n+1
i = i + 1; n
sum = sum + i; n
}
while (!done)
{
result = result * n;
n--;
if (n <= 1) done = true;
}
19
Reminder: Lab Project
• Choose an Interesting topic relevant to DSA
21