This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Programming & Data Structures Tutorial 7 – Sample Solutions Q1 (i) f(n) = 1 + N + N + N + N + N + N = 1+ 6N = O(N) (ii) f(n) = 1 + lgN = O(lgN) Reason why it it lgN here is because variable i is doubled during each iteration. So if we let N = 2 n this is equivalent to n = lgN. (iii) The inner loop is dependant on the outer loop so in this instance we can take both loops as the one loop. So for each iteration we end up with something along the lines : f(n) = 1 + 2 + 3 + … + (n-1) + n Using Gauss’ equation we can simplify this summation to : f(n) = (N 2 + N) / 2 = O(N 2 ) (iv) f(n) = 1 + N + N + N * f(func2) f(func2) = O(lgN) from part (ii) above so : f(n) = 1 + 2N + N*O(lgN) = O(N*lgN) Q2 - linear search = O(N) - binary search = O(lgN) - bubble sort = O(N 2)- insertion sort = O(N 2)- selection sort = O(N 2) Q3 Show that 2 n 2 + 4 n +3 is O(n 2 ) 0 ≤ f ( n ) ≤ cg ( n ) for n > n f ( n ) = 2 n 2 + 4 n +3 g ( n ) = n 2 If n = 2 and c = 5 then 0 ≤ f ( n ) ≤ cg ( n ) 0 ≤ 19 ≤...
View Full Document
This note was uploaded on 05/25/2010 for the course CPE CPE 360 taught by Professor Jenniferchen during the Spring '10 term at Stevens.
- Spring '10