Note that there are some explanatory texts on larger screens.

plurals
  1. POComputational complexity of a algorithm
    primarykey
    data
    text
    <pre><code>Algorithm 1. STACKSTUFF(n) Input: Integer n 1) Let S = an empty Stack 2) Let X = -1 3) For i = 1 to 2n 4) S.Push(i) 5) End For 6) For i = 1 to n 7) Let X = S.Pop() 8) End For Output: The contents of X </code></pre> <hr> <pre><code>1) What is this algorithm written in pseudo code doing? </code></pre> <p>To my understanding, S.Push(i) adds item i on top of stack S. X = S.Pop() removes the item from the top of stack S and assigns it to X.</p> <pre><code>2) What is the computational complexity O(n) for algorithm 1, STACKSTUFF? </code></pre> <p>I believe the answer would be: O(3n)</p> <p>The first loop would be 2n and the second for loop n, so 2n+n=3n.</p> <p>Or... Would the answer just be O(n^2) since all we would have to do would be n*n?</p> <pre><code>3) If n &gt; 0 then what is returned by the algorithm? What about n &lt; 1 a) 2n b) -1 c) n-1 d) n+1 e) None of the above </code></pre> <p>This last bit really confuses me. From my understanding, if n was always greater than 0, the algorithm would always return n+1, and if n was always less than 1, the algorithm would return n-1. However this is pure guess work...</p> <p>If I thought about this logically, then let's say n was 3 for example. Since the first For loop is 1 to 2n, then this would mean that we would end up with the following stack S={1,2,3,4,5,6} as it added every number up to double n into S. The second For loop then pops 3 numbers so X ends up looking like this X={6,5,4}. If I am correct there... Should I assume that this was just a trick question and the answer is e, none of the above?</p> <p>I just wanted to make sure my understanding here was correct before I continued studying. Thanks for any help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload