complexity

complexity - 1 I I . . ; ,O | 1 1.1 . gn () fn () .1 n...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 I I . . ; ,O | 1 1.1 . gn () fn () .1 n > n0 n0 c gn () O(g (n)) fn () fn () c gn () : n 5 n0 4n2 < n c n 2 = n3 () =1 c=3 n0 = 5 c=1 n0 gn 2n = O(n) 4n2 = O(n3 ) (g (n)) ( ) () fn n > n0 () () .2 fn c gn () : c = n0 = 1 1 n0 = 1 c= 7 .f n = (n) n= (7 log n) n 2 ( ) () fn ( ) = (g (n)) ( ) = O(g (n)) .2n = (n) fn gn () 2n = O(n) O; ; (g (n)) : () .3 1.2 g n O(n2 ) n f c c n- 2 O(n2 ) n 2 O c n n (n2 ) c 2 1 1 I 1.3 k Pn () .1 Pn ( )= k X i=0 ai n i ai 0; ak > 0 Pn ( ) = O(nk ) Pn ( )= k X i=0 ai n i k X i=0 ai n k = nk k X i=0 ai k X i=0 ai ; n0 ai ! c = k X i=0 =1 Pn ( ) = O(nk ) Pn Pn ( ) = (nk ) k?1 X i=0 ai n () .2 Pn ( )= k X i=0 ai n i = a k nk + n0 i ak n k c = ak ; =1 Pn ( ) = (nk ) Pn ( ) = (nk ) .3 k 2 n X k i i=0 = O(kn ) n X k i i=0 n+1 k 1 = k k ?? 1 = k ? 1 kn ? k ? 1 1 n X k 2 k n i i=0 = O(kn ) 2 1 I 2 f2 n () f1 n f2 n () ( ) = O(g2(n)) fn f1 n ( ) = O(g1(n)) .1 2 ( ) = f1 (n) + f2 (n) g2 n f1 n ( ) + f2 (n) = O(g1(n) + g2 (n)) = O(maxfg1(n); ( )g) 9 9 ( ) + f2 (n) c1 ; n 1 c2 ; n 2 8 8 n > n1 n > n2 f1 n f2 n () () c1 c2 g1 n g2 n () () n> maxfn1 ; n2 g f1 n c1 g1 n ( ) + c2 g2 n ( ) maxfc1; c2g (g1(n) + g2(n)) f1 n ( ) + f2 (n) = O(g1 (n) + g2(n)) = maxfc1 ; c2g; n0 = maxfn1 ; n2 g g1 (n) + g2 (n) 2 maxfg1(n); g2 (n)g f1 (n) + f2 (n) = O(g1 (n) + g2 (n)) = O(maxfg1 (n); g2 (n)g) c fn ( ) = f1 (n) f2 n () f1 n .2 () f2 n ( ) = O(g1(n) maxfn1 ; ) (g1(n) g2 n ( )) n> n2 g g .3 f1 n () f2 n ( ) (c1 g1 n ( )) (c2 ( )) g2 n ( )) = (c1 = c1 c2 g2 n ( )) n2 f1 n () f2 n ( ) = O(g1(n) g2 n c c2 ; n0 = maxfn1 ; f1 n ( )f2 (n) = O(g1 (n)g2(n) ) ( ) = g2(n) = n n 2n = O(n) f1 n f1 n ( ) = f2 (n) = 2n; n g1 n ( )f2 (n) = (2n)2n = 22n 22n n 2n = 22n n n n n c n 22n >c n n n n>c c n n (2n)2n = O(nn ) 6 3 1 I 3 O(1) 1. scanf (\%d", &n); n Tn () k = 2; while (k n) f k += 1; g < k i k ki (0) = 2 k (i) = k (i ? 1) + 1 = k (i ? 2) + 2 = : : : = k (0) + i = 2 + i k (i) = 2 + i < n T (n) = O(n) 2. scanf (\%d", &n); () (= i n ?2 k = 2; while (k n) f k = k * 2; g < (0) = 2 ( ) = k(i ? 1) 2 = (k(i ? 2) 2) 2 = k(i ? 2) 22 = : : : = k(0) 2i = 2i+1 2i+1 n =) log2 2i+1 = i + 1 log2 n =) T (n) = O(log n) k ki 3. scanf (\%d", &n); k = 2; while (k n) f k = k * k * k; g < (0) = 2 ? 3 33 k (i) = (k (i ? 1)) = (k (i ? 2)) = (k(i ? 2))32 = : : : = k(0)3 = 23 23 n =) 3i log2 n =) i log3 log2 n =) T (n) = O(log log n) k i i i 4. scanf (\%d", &n); > k = n; while (k 1) f k = oor (k / 5); for (j = 1; j = log (n); j++) for (i = 1; i = oor (n / 2); i++) x += 1; g < < 4 1 I for n log5 n while 2 Tn ( ) = O( n log2 n 2 for log2 n log5 n) = O(n log2 n) 5. scanf (\%d", &n); < < k = 2; for (i = 1; i = n; i++) f for (j = 1; j = i; j++) x += 1; while (k i) k = k * k; g < while - i i i for O(1) n nn O(i)+O(1) = O(i) Tn ( )= n X i=1 O(i) = O n X i=1 ! i =O ( + 1) = O 2 n 2 6. int array n]; void bubble (int *array, n) f int i, j, temp; for (i = 0; i n; i++) for (j = n ? 1; j i; j??) if (array j?1] array j]) f temp = array j?1]; array j?1] = array j]; array j] = temp; g < > > g Tn ( )=O ? n 2 5 1 I 4 O(1) O(log n) O(log2 n) O(log n) k . . . . . . 9 > > > > = > > > > ; 9 > > > > > > > > > > > > > > > > > > > > > = > > > > > > > > > > > > > > > > > > > > > ; O(n) O(n2 ) O( ) k n . . . . . . 9 > > > > = > > > > ; O(2log2 n ) O(2log n ) k . . . 9 > > = > > ; 9 > > > > = n log n O(2n ) O(3n ) n n . . . . . . O( ) = O(2 O(22 ) n . . . ) > > > > ; ) . . . 6 ...
View Full Document

Ask a homework question - tutors are online