Sort a small stack
Input
stack = [3, 1, 4, 2] # top is 2 (last element)
Output
[4, 3, 2, 1]
After sorting, the smallest element (1) should be on top (end of list). The list reads bottom->top: 4,3,2,1.
Full lesson preview
Sort a stack using only one extra stack and no other data structures.
Problem statement
Task
Examples
Input
stack = [3, 1, 4, 2] # top is 2 (last element)
Output
[4, 3, 2, 1]
After sorting, the smallest element (1) should be on top (end of list). The list reads bottom->top: 4,3,2,1.
Input format
Output format
Constraints
Samples
Input
stack = [1, 2, 3, 4]
Output
[4, 3, 2, 1]
Regardless of initial order, after sorting the smallest element is at the top (end).