3-IterAlg.pptx - CS 3343 Analysis of Algorithms Analyzing non-recursive algorithms 1 True or false 1 2 3 4 5 6 2n2 1 = O(n2 T(also Sqrt(n = O(log n F

3-IterAlg.pptx - CS 3343 Analysis of Algorithms Analyzing...

• Lecture Slides
• 23

This preview shows page 1 - 10 out of 23 pages.

5/25/18 1 CS 3343: Analysis of Algorithms Analyzing non-recursive algorithms
5/25/18 2 True or false? 1. 2n 2 + 1 = O(n 2 ) T (also ) 2. Sqrt(n) = O(log n) F ( ) 3. log n = O(sqrt(n)) T (also o) 4. n 2 (1 + sqrt(n)) = O(n 2 log n) F ( ) 5. 3n 2 + sqrt(n) = O(n 2 ) T (also 6. sqrt(n) log n = O(n) T (also o)
5/25/18 3 True or false? 1. 2n 2 + 1 = O(n 2 ) T (also ) 2. Sqrt(n) = O(log n) F ( ) 3. log n = O(sqrt(n)) T (also o) 4. n 2 (1 + sqrt(n)) = O(n 2 log n) F ( ) 5. 3n 2 + sqrt(n) = O(n 2 ) T (also ) 6. sqrt(n) log n = O(n) T (also o)
5/25/18 4 Analyzing the complexity of an algorithm
5/25/18 5 Kinds of analyses Worst case Provides an upper bound on running time Best case – not very useful, can always cheat Average case Provides the expected running time Very useful, but treat with care: what is “average”?
5/25/18 6 General plan for analyzing time efficiency of a non-recursive algorithm Decide parameter (input size) Identify most executed line (basic operation) worst-case = average-case? T(n) = i t i T(n) = Θ (f(n))
5/25/18 7 Example repeatedElement (A, n) // determines whether all elements in a given // array are distinct for i = 1 to n-1 { for j = i+1 to n { if (A[i] == A[j]) return true; } } return false;
5/25/18 8 Example repeatedElement (A, n) // determines whether all elements in a given // array are distinct for i = 1 to n-1 { for j = i+1 to n { if (A[i] == A[j]) return true; } } return false;
5/25/18 9 Best case?