complexity for recursion function -


i have written function reverse stack inline. these 2 member function of stack class .

void reverse() {     int first=pop();     if(first!=-1)     {         reverse();         insert(first);     } } private: void insert(int i) {     int temp=pop();      if(temp==-1)     {        push(i);          }     else     {         /* there element in stack*/        insert(i);        push(temp);      } } 

now how can analyze function in form of big o calculate complexity.

your insert() takes o(length of stack) time because:

t(n) = t(n-1) + o(1)[to push] = o(n) 

and reverse() takes o(square of length of stack) time because:

t(n) = t(n-1) + o(n)[for insert] = o(n^2) 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -