Thursday, March 7, 2013

[Cracking the Code Interview 4th Edition] 3.6

Write a program to sort a stack in ascending order.

Thought:
Use another stack r & the original stack is s.

while(s is not empty) {
      val = pop from s
      while (val is smaller than peeking r) {
            push to s(pop from r)
      } 
      push val to r
}

No comments:

Post a Comment