Note that there are some explanatory texts on larger screens.

plurals
  1. POBest case, average case, worst case, and amortized case analysis
    primarykey
    data
    text
    <p>I am looking of an understanding of how things work, not just the answer. I'm giving what I've though the answer is, but I'm not really sure and the text doesn't have much on this kind of analysis.</p> <p>I am quite aware that this NOT how you are supposed to implement a stack, but this is the scenario we have been given.</p> <p>Part A</p> <blockquote> <p>What is the worst case, average case, and best case running time of insertion into slot a[0] of an (unsorted) array a containing N integers, representing a stack? Give theta bounds. </p> </blockquote> <p>Part B</p> <blockquote> <p>What is the amortized running time of insertion of N integers into an initially empty array a, representing a stack, if every insertion takes place at a[0]? Give a tight O bound and explain your answer.</p> </blockquote> <p>The instructor was quite clear that we are doing all insertions into a[0], and there are no removals from this "stack".</p> <p>My analysis is this</p> <p>Assumption: a.length >> N</p> <p>Best case is Theta(1) which occurs when we insert into an empty "stack".</p> <p>Worst Case is Theta(N) because as part of the insertion process we have to shift N-1 elements to make room at a[0].</p> <p>Average case is also Theta(N) because, no matter what, we always have to shift N-1 elements.</p> <p>Amortized case</p> <p>Each insertion costs (N-1) and we have N elements, so we use:</p> <pre><code>N Sum (i) = [ N(N+1) ] / 2 = N^2 / 2 which is O(N^2) for N insertions i=1 </code></pre> <p>Amortized cost would then be N^2 / N = O(N) </p> <p>Problem I've having is that this looks to easy. Am I missing something, or do I have it pretty much dead on?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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