Last active
July 21, 2017 19:17
-
-
Save ggodreau/118e81734ba1c706c2b634c954fdee51 to your computer and use it in GitHub Desktop.
Simple python stack class with a sorting method
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
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