Vous êtes sur la page 1sur 2

CSC 165 H1Y Worth: 3%

Homework Exercise # 8

Summer 2012 Due: By 3 pm on Tuesday August 7.

1. The following is one of possible implementation of insertion sort: # Precondition: A is a list of n numbers and n = len(A) for i in 0, 1, ..., n-1: for j in i-1, i-2, ..., 0: if A[j] < A[j+1]: tmp = A[j] A[j] = A[j+1] A[j+1] = tmp else break

1. 2. 3. 4. 5. 6. 7. 8.

Give a tight bound on the worst-case running time of the above algorithm, and write a detailed proof that your bound is correct. Let us call the worst-case runtime for the above algorithm W (n). Then W (n) (n2 ). This is equivalent to saying that W (n) O(n2 ) W (n) (n2 ), so we can prove both parts separately. W (n) O(n2 ) c R+ , B N, n N, n B W (n) cn2 Let I be a set of valid inputs to the algorithm. Let t(x) be the time taken by the algorithm on an input x. (Note: this nomenclature is standard in this course, so it can be used without explicit introduction). W (n) is the biggest value of t(x) over inputs x of size n. So, W (n) cn2 x I, (size(x) = n) t(x) cn2 So, we want to prove the following: c R+ , B N, n N, n B x I, (size(x) = n) t(x) cn2 Assume n N and n 0 Assume A is an array of numbers, and size(A) = n Then, the outer loop would execute n times. For each of these executions, the inner loop would execute no more than n times. (usually, much less, but we only need a bound here) Each execution of the inner loop would take no more than 5 steps: executing lines 2-6, or 2,7-8. So, t(A) n(1 + 5n) = n + 5n2 6n2 Then, x I, (size(x) = n) t(x) 6n2 Then, n N, n 0 x I, (size(x) = n) t(x) 6n2 # Now, use Existential Introduction, take B = 0, 0 N, and c = 6, 6 R+ Then, c R+ , B N, n N, n B x I, (size(x) = n) t(x) cn2 Then, W (n) O(n2 )

Note: there are many ways of counting lines for the algorithm. We may or may not count the last execution of the for loop, the implicit goto in the if-statement, etc. Any reasonable and consistent way is ok, and should give the same bound.

Dept. of Computer Science, University of Toronto, St. George Campus

Page 1 of 2

CSC 165 H1Y Now, we want to prove W (n) (n2 ).

Homework Exercise # 8

Summer 2012

W (n) (n2 ) c R+ , B N, n N, n B W (n) cn2 And W (n) cn2 means that the maximum time over inputs of size n is at least cn2 which is equivalent to saying that some input of size n has a runtime of at least cn2 . Then, W (n) cn2 x I, (size(x) = n) t(x) cn2 So, we want to prove c R+ , B N, n N, n B x I, (size(x) = n) t(x) cn2 Assume n N and n 2 Let A be an array [0, 1, 2, 3, ..., n 1] an array of integers from 0 to n 1. Then, size of A is n. Then, the outer loop would execute n times. At the beginning of each outer iteration, the element at A[i] will be larger than all the elements in A[0]...A[i 1]. So, every time the concdition on line 3 will be true, and the element would be swapped, until the element which was at A[i] is at A[0]. So, the inner loop would execute i times, with each execution performing lines 2 6, and taking 5 steps. So, the inner loop would take 5i steps. So, t(A) = (1 + 5 0) + (1 + 5 1) + (1 + 5 2) + ... + (1 + 5(n 1)) Then, t(A) =
n1 i=0 (1

+ 5i) = (n 1) + 5

n1 i=0 i

= n 1 + 5 n(n1) 2

5 = (n2 n) + n 1 2 5 3 = n2 n 1 2 2 5 2 3 2 1 2 n n n #n2 2 4 4 5 2 n n2 2 3 = n2 2 So, t(A) (3/2)n2 Then, x I, (size(x) = n) t(x) (3/2)n2 Then, n N, n 2 x I, (size(x) = n) t(x) (3/2)n2 # Now, use Existential Introduction, take B = 2, 2 N, and c = (3/2), (3/2) R+ Then, c R+ , B N, n N, n B x I, (size(x) = n) t(x) cn2 Then, W (n) (n2 )

So, W (n) (n2 )

Dept. of Computer Science, University of Toronto, St. George Campus

Page 2 of 2

Vous aimerez peut-être aussi