This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: recurrence is T ( n ) = 2 T ( n 2 ) + cn Using Master's Theorem, we conclude that T ( n ) is O ( n log n ). Next, if we choose rst element of input as a pivot value, we will always choose the lowest because it is already sorted. So partition will always be unbalanced for every step. The recurrence is T ( n ) = 2 T ( n 1) + cn; n > 1 1 Using Master's Theorem, we conclude that T ( n ) is O ( n 2 ). Problem 5 (10pts) Design: Let's de ne additional array T whose size is at least the maximum value of list S. (Actually, we don't know the maximum value of S. But because we don't have any limitation on storage, we could assume T has a in nite index.) Then the values in array T are decided by the following rules: 1. The value of all elements in T is intially 0. 2. The value in T is 1 only for the element whose index is S[k](0 k n 1). Shortly, T[S[k]] = 1. Now we could nd out whether there exist two elements in S whose sum equals x by the following procedure. FindIt for(k = 0; k < n; k++) f index = xS[k]; if((index 0) and (T[index] == 1))s return true; g return false; Complexity: From the algorithm above, we could know that the worst case is when there is no such two elements in S whose sum equals x. For that case we should check all of the elements in S. Since there is n integers in S, the complexity is O(n) in worst case. Problem 6 (10pts) Let's say that DeleteMin(S), push(S), pop(S) each take O(1). Then if you push n elements in the stack and subsequently do DeleteMin n times, you will have a sorted output in O(n) since you will do n times O (1) operation. But we know from the following theorem(p249 in text book) that lower bound is ( n log n ) for sorting using comparison. Theorem 7.7: Any sorting algorithm that uses only comparisons between elements requires ( n log n ) comparisons. 2 So we do not believe the Professor. 3...
View
Full Document
 Spring '09
 Algorithms, Data Structures, Tn, running time, Divide input array

Click to edit the document details