cse220sol2

Ni n 2 if n 1 = ¢ n 2 then the rst algorithm is o n

Info iconThis preview shows pages 2–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 ( nd-min 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 nd-min return the value at the top of the auxiliary stack (without popping it). 3...
View Full Document

{[ snackBarMessage ]}

Page2 / 3

Ni N 2 If N 1 = ¢ N 2 then the rst algorithm is O N 1 and...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online