Lecture 2 - Complexity Analysis
Lecture 2 - Complexity Analysis
Based on the reference materials by Prof. Goodrich, OCW METU and Dr. Vidhya Balasubramanian
• Model of Computation
̶ Mathematical Framework
• Asymptotic Notation
̶ What to Analyze
• Running Time Calculations
• Checking the analysis
̶ Calling a method
̶ Comparing two numbers
̶ Indexing into an array
̶ Following an object reference
̶ Returning from a method
• Loops
̶ cost is linear in terms of number of iterations
̶ Nested loops – product of iteration of the loops
o If outer loop has n iterations, and inner m, cost is mn
• If Else
̶ Cost of if part of else part whichever is higher
• Algorithm arrayFind(x,A);
//Given an element x and an n element array A, output pos if present in A
for i = 0 to n-1 do
if x = A[i] then
return I
return -1
• There is an algorithm find2D to find an element x in an nxn
array A. The algorithm iterates over the rows of A and calls the
algorithm arrayFind on each one, until x is found or it has
searched all rows of A.
̶What is the time and space complexity of the algorithm