midtermsol

# Recurrence is t n = 2 t n 2 cn using master's theorem

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
This is the end of the preview. Sign up to access the rest of the 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 = x-S[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

{[ snackBarMessage ]}

### Page1 / 3

recurrence is T n = 2 T n 2 cn Using Master's Theorem we...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online