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
Post a Comment