2c03-review - 00093

2c03-review - 00093 - 20000 j=n O(1)+ i j=1 O(n)) = n i=1...

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

View Full Document Right Arrow Icon
6 T 1 (n) = C + 2*T 1 (n - 3) = (1+2)C +2 2 T 1 (n – 3*2) = (1+2+2 2 )C +2 3 T 1 (n - 3*3)…. = (1+2+2 2 +…+2 n/3-1 )C +2 n/3 T 1 (1) where m ~ logn/log3 = 2 n/3 *C + 2 m C’ = O(2 n/3 ) T 2 (n) = C + 2*T 2 (n - 1) = (1+2)C +2 2 T 2 (n - 2) = (1+2+2 2 )C +2 3 T 2 (n - 3)…. = (1+2+2 2 +…+2 (n-1) )C +2 n T 1 (1) where m~logn = 2 n C + 2 m C’ = O(2 n ) So, T 1 (n) < T(n) < T 2 (n), and T(n)=O(T 2 (n))= O(2 n ) d.[5] Below the function F(n) is the function from (c) above procedure P3(n:integer); var i,j,x,y : integer; begin for i:=1 to n do if i mod 3 = 1 then begin for j:=i to n do x:=x+1; for j:=20000 downto n do x:=x+y; for j:=1 to i do F(n) end end T(n) = Σ n i=1 T1 = Σ n i=1 1/3 *(T2+T3+T4) = Σ n i=1 ( Σ n j=i O(1) +
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 20000 j=n O(1)+ i j=1 O(n)) = n i=1 (O(n-i) +O(1)+ O(i*2 n )) = O(n*2 n ) 6.[20] Write procedures to implement the following operations on singly linked lists. What is the time complexity of each operation? Assume a pointer implementation. a.[5] 3ONLY(L). The procedure deletes from L all elements except those on 3k+1, k $ 0, positions. For instance if L=2,3,1,5,7,8,9 then 3ONLY(L) = 2,5,9, i.e. only positions 1,4,7 are left. procedure 3ONLY(var L: celltype;); var p : celltype; var n : integer; begin p := L; n:=1;...
View Full Document

Ask a homework question - tutors are online