Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def sort(stack):
- if len(stack) == 1:
- return stack
- mid = len(stack)//2
- oStack = []
- for i in range(mid): # move top half to other stack
- oStack.append(stack.pop())
- sort(oStack)
- sort(stack)
- fStack = [] # Merge into final stack
- while len(stack) > 0 and len(oStack) > 0:
- if stack[-1] < oStack[-1]:
- fStack.append(stack.pop())
- else:
- fStack.append(oStack.pop())
- while len(stack) > 0:
- fStack.append(stack.pop())
- while len(oStack) > 0:
- fStack.append(oStack.pop())
- while len(fStack) > 0: # Move everything from final stack to stack as it is in reverse on final stack
- stack.append(fStack.pop())
- return stack
- print(sort([1,2,5,1,3,7,8,1,2,10,12,7]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement