100 likes | 231 Views
Algorithm Efficiency . Algorithm efficiency A function of the number of elements to be processed f(n)=efficiency If a function is linear – that is, if it contains no loop – then its efficiency is a functions of the number of instructions it contains.
E N D
Algorithm Efficiency • Algorithm efficiency • A function of the number of elements to be processed • f(n)=efficiency • If a function is linear – that is, if it contains no loop – then its efficiency is a functions of the number of instructions it contains. • On the other hand, functions that loop vary widely in their efficiency.
Algorithm Efficiency • Linear loop n=1000 i=1 loop (i<=1000) application code i=i+1 end loop f(n)=n
Algorithm Efficiency • Logarithmic loop • Multiply 2 iteration < 1000 i=1 loop (i<1000) application code i=i x 2 end loop f(n)= log2 n
Algorithm Efficiency • Logarithmic loop • Divide 1000 /2 iteration >=1 i=1000 loop (i>=1) application code i=i / 2 end loop f(n)= log2 n
Algorithm Efficiency • Nested loop • Iterations = inner loop iterations x outer loop iterations • Linear logarithmic i=1 loop (i<=10) j=1 loop (j<=10) application code j= j x 2 end loop i=i + 1 end loop 10 f(n)= n [log2 n] log2 10
Algorithm Efficiency • Nested loop • Quadratic i=1 loop (i<=10) j=1 loop (j<=10) application code j= j +1 end loop i=i + 1 end loop 10 f(n)= n2 10
Big-O Notation The simplification of efficiency is known as big-O analysis. We don’t need to determine the complete measure of efficiency, only the factor that determines the magnitude. This factor is the big-O, and expressed as O(n) – that is, on the order of n.
Big-O Notation • The big O notation can be derived from f(n) using the following steps: • In each term, set the coefficient of the term to 1. • Keep the largest term in the function and discard the others. Terms are ranked from lowest to highest as shown below: Log2n n nlog2n n2 n3… nk 2n n!
Big-O Notation • Example: • Calculate the big-O notation for: • f(n) = n [(n+1)/2] Solution: ½ n2 + ½ n (Remove the coefficient) n2 + n (keep the largest term) n2 So, the big-O notation is stated as O( f(n) ) = O(n2)