Java Stack array - Big O notation -


what big o notation code below. still couldn't grasp concept fully. supposed thought experienced coders give summary big o performance based on code.

import java.util.*; import java.util.inputmismatchexception; import javax.swing.*; public class mystack {     private int maxsize;    private long[] stackarray;    private int top;    public mystack(int s) {       maxsize = s;       stackarray = new long[maxsize];       top = -1;    }    public void push(long j) {       stackarray[++top] = j;       system.out.println("push onto stack");    }    public long pop() {       return stackarray[top--];    }    public long peek() {       return stackarray[top];    }    public boolean isempty() {       return (top == -1);    }    public boolean isfull() {       return (top == maxsize - 1);    }    public static void main(string[] args) {         scanner num = new scanner(system.in);         int input =0;         int x;         mystack thestack = new mystack(5);      for(x=0; x<5; x++)     {           system.out.println("\nenter number(push): ");         input = num.nextint();         thestack.push(input);          }          system.out.print("the first element on top top of stack");      system.out.println("");       while (!thestack.isempty()) {          long value = thestack.pop();          system.out.print(value);          system.out.println(" pop");        }         system.out.println("");     } } 

big o performance varies operation you're attempting. @ "isempty()" method. looks @ value of top, that's constant, or o(1). don't see other methods in class (except main(), we'll @ in minute) have dependency on number of elements in array, work top.

main() asks 5 values, prints them out. if asked 50, take ten times longer (assuming user input remained relatively constant). main o(n) n number of elements in array.

if looking particular number in array, you'd have examine each 1 in turn, o(n).

and if doing more complicated looked @ each element , did operation or comparison each other element (e.g. nested loop) you'd end o(n^2).

hope helps thought process.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -