This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Ni; N 2))) If N 1 = ¢( N 2) then the rst algorithm is O ( N 1), and the second is O ( N 1 log N 1). So, in this case the rst is faster. 2 If N 1 = o ( N 2) then the rst algorithm takes O ( N 2), and the second takes O ( N 1 log N 2). So, in this case the second is faster. Problem 4: Grade 7.5 + 10 pts This problem requires you to implement a special stack. The special stack does the usual push and pop in constant times. In addition, it must nd the minimum value of elements currently in stack in constant time ( ndmin operation). There are di erent solutions. One of them is the following: Use an implementation of a stack along with an auxiliary stack to keep track of the minimum element in the stack at each point. Updating of the auxiliary stack may either be done at every operation or only when the minimum in the stack changes. Let's take the implementation that keeps both stacks at the same height. Under this implementation, when doing a push , we compare the element x to be pushed with the top of the auxiliary stack y (representing the current minimum). If x < y we push x on to both stacks. Otherwise we push x on to the regular stack and y on to the auxiliary stack. When performing a pop simply pop both stacks and return the value popped from the regular stack. When performing a ndmin return the value at the top of the auxiliary stack (without popping it). 3...
View
Full Document
 Spring '09
 Algorithms, Data Structures, The Return, OTH, 12rst element t, element t position, 12rst stk nd

Click to edit the document details