Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created October 8, 2020 16:32
Show Gist options
  • Save codecakes/3b1bbb187ddf99e3d180081394770aac to your computer and use it in GitHub Desktop.
Save codecakes/3b1bbb187ddf99e3d180081394770aac to your computer and use it in GitHub Desktop.
Sort Stack in ascdending order as more stacks increase by value, with Lowest value at top
"""Lowest value at top, highest on top."""
from Stack import MyStack
def sort_stack(stack):
if not stack.is_empty():
top_val = stack.pop()
sort_stack(stack)
_insert(stack, top_val)
return stack
def _insert(stack, top_val):
if not stack.is_empty() and top_val > stack.top():
temp = stack.pop()
_insert(stack, top_val)
top_val = temp
stack.push(top_val)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment