100 likes | 189 Views
Learn about analyzing algorithm run time complexity, input size, number of basic operations, worst and every case scenarios, Big Oh notation, efficiency analysis, and examples like sorting and adding numbers.
E N D
Algorithm Analysis Run time complexity
Analysis Concepts • Input Size • Number of input units • Sorting – List length • Adding 2 numbers – Number digits • Running Time • Number of basic operations • Sorting – Comparison • Adding 2 numbers – Computing sum & carry
Worst Case / Every Case • Every case refers to algorithms whose run time do not depend on input but only size. Examples : Adding sequence of numbers, etc. • Worst case refers to algorithms whose run time does depend on input as well as size. Examples : Insertion Sort
Big Oh Notation • A function f(n) is O(g(n)) iff • There is an integer no and • There is a constant c such that • f(k) < c g(k) when k > no • Show that an2 + bn + c is O(n2)
Analysis of MaxElement • Determines value of largest element in array • Maxval = A(0) • For I = 1 to n-1 • If A(I) > Maxval • Maxval = A(I) • Return Maxval
Unique Element • For I = 0 to n-1 • For J = I+1 to n-2 • If A(J) = A(I) return false • Return True
Efficiency Analysis • Measure input size • Identify basic operation • Determine whether number of times basic operation is every case or worst case • Calculate number of times T(n) basic operation is executed as function of input size n • Determine Order of T
Analysis of MaxElt and UniqueElt • MaxElt • Has to go thru entire array, regardless of values • Every Case • O(n) • UniqueElt • May detect false on 1st comparison or last • Best Case / Worst Case • O(1) / O(n2)