Sort a Stack Using a Temporary Stack

Problem Sort a stack ascending (smallest on top or bottom — fix the convention) using ONLY one auxiliary stack — no arrays, no recursion.

Input / Output

  • Input: stack of ints. Output: the same stack, sorted.

Constraints

  • Up to 10^4 elements; O(n^2) worst case is expected and accepted — the constraint is the data-structure discipline, not asymptotics.

Example

  • [34,3,31,98,92,23] (top=23) → sorted stack [3,23,31,34,92,98].
asked …
LeaderboardSalaryAccount