2c03-review - 00031

# 2c03-review - 00031 - k then n n = n n/2 × n n/2 exp(a,b...

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

5 T(n) = if n 1 then C 1 else C + T(n-1) +T(n div 2) Let T 1 (n) and T 2 (n) be the following functions: T 1 (n) = if n 1 then C 1 else C + 2T 1 (n-1), T 2 (n) = if n 1 then C 1 else C + 2*T 2 (n div 2). Clearly T 2 (n) < T(n) < T 1 (n) if n>1. Note that, for n > 1: T 1 (n) = C + 2T 1 (n-1) = (1+2)C +2 2 T 1 (n-2) = (1+2+2 2 )C +2 3 T 1 (n-3)…. = (1+2+2 2 +…+2 (n-2) )C +2 (n-1) T 1 (1) = (2 (n-1) -1)C + 2 (n-1) C’ = O(2 n ) T 2 (n) = C + 2*T 2 (n div 2) = (1+2)C +2 2 T 2 (n div 2 2 ) = (1+2+2 2 )C +2 3 T 2 (n div 2 2 )…. = (1+2+2 2 +…+2 (m-1) )C +2 m T 1 (1) where m~logn = (2 m -1)C + 2 m C’ = O(2 m ) =O(n) Hence, T 2 (n) < T(n) < T 1 (n), and T(n)=O(T 1 (n))= O(2 n ) 5.[10] Write a pseudo-Pascal program to compute n n with O(log n) complexity. The idea is based on the following observation: for real numbers or if n= 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: k , then n n = n n/2 × n n/2 . exp(a,b) var q:^celltype; begin if b=0 then return (1) else if b mod 2 = 1 then exp(a,b):= a*(exp(a,b div 2)) 2 else exp(a,b):=(exp(a,b div 2)) 2 end Then, exp(n,n) calculate n n , and the time complexity is O(logn). The program does not need to be the recursive one, but the recursive one is the most obvious. 6.[10] Assume “a few” = O(1). Then insertion sort gives time=O(n) Assume “a few” # , then if heap sorts the random elements first and then merge with n the rest: time=O(n), space=O(n)....
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online