Skip to content

Instantly share code, notes, and snippets.

@ggodreau
Last active July 21, 2017 19:17
Show Gist options
  • Save ggodreau/118e81734ba1c706c2b634c954fdee51 to your computer and use it in GitHub Desktop.
Save ggodreau/118e81734ba1c706c2b634c954fdee51 to your computer and use it in GitHub Desktop.
Simple python stack class with a sorting method
class Stack:
def __init__(self):
self.data = []
def push(self, item):
self.data.append(item)
def pop(self):
return self.data.pop()
def show(self):
return self.data
def selectionSort(self):
# mutates the original stack, sorts ASC, only handles numerics
# could probably use some error handling
# adapted from interactivepython.org
for fillslot in range(len(self.data)-1,0,-1):
positionOfMax=0
for location in range(1,fillslot+1):
if self.data[location]>self.data[positionOfMax]:
positionOfMax = location
temp = self.data[fillslot]
self.data[fillslot] = self.data[positionOfMax]
self.data[positionOfMax] = temp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment