#  O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop.

## Presentation on theme: " O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop."— Presentation transcript:

 O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop in a loop: O(n 3 )

for (int i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4035705/slides/slide_3.jpg", "name": "for (int i=0; i

 T(n) = n 2 + 5n + 25 Or T(n) = O(f(n)) These exists two constants, n0 and c (>0) and a function f(n) such that all n>n0, cf(n)  T(n). Translate as: If n gets sufficiently large, there is some constants c for which processing time will always be less than or equal to cf(n). cf(n) is an upper bound on the performance.

 The growth rate of f(n) will be determined by the growth rate of the fastest growing term  It’s safe to ignore all constants and drop the lower order terms when determining the Big O for an algorithm

for (int i=0; i< n-1; i++) { for (int j=i+1; j { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4035705/slides/slide_6.jpg", "name": "for (int i=0; i< n-1; i++) { for (int j=i+1; j

for (i=0; i< x.length; i *=2) { // print out i } -The loop body will execute k-1 times with i: 1,2,4,8,16… until 2 k > x.length -2 k-1 <= x.length < 2 k -K-1 <= log 2 (x.length) < k -So this loop has O(log 2 n)

1. for (int i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4035705/slides/slide_9.jpg", "name": "1. for (int i=0; i

3. for (int i=0; i=i; j--) System.out.println(i+” “+j); 4. for (int i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4035705/slides/slide_10.jpg", "name": "3. for (int i=0; i=i; j--) System.out.println(i+ +j); 4.", "description": "for (int i=0; i

for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) for (int k=n; k>=1 ; k--) { Int sum = i+j+k; }

 If T(n) is the form of a polynomial of degree d (d is the highest exponent), then it is O(n d ).  O(1) represents a constant growth rate. This value doesn’t change with the number of inputs. Any finite number of O(1) steps is still O(1)

public static boolean areUnique(int[] x) { for(int i=0; i< x.length; i++) { for (int j=0; j { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4035705/slides/slide_13.jpg", "name": "public static boolean areUnique(int[] x) { for(int i=0; i< x.length; i++) { for (int j=0; j

Big OName O(1)Constant O(log n)Logarithmic O(n)Linear O(n log n)Log-linear O(n 2 )Quadratic O(n 3 )Cubic O(2 n )Exponential O(n!)Factorial

15

Download ppt " O: order of magnitude  Look at the loops and to see whether the loops are nested. ◦ One single loop: O(n) ◦ A nested loop: O(n 2 ) ◦ A nested loop."

Similar presentations