Created
March 9, 2016 22:04
-
-
Save aniruddha84/e54753e1dc3790ffbf3c to your computer and use it in GitHub Desktop.
Sort a stack of integers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def stack_sort(stack) | |
result = [] | |
until stack.empty? | |
t = stack.pop | |
while !result.empty? && result.last > t | |
stack.push result.pop | |
end | |
result.push t | |
end | |
result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment