Chapter 3 Stacks and Queues - 3.6

Problem 3.6: Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to write this program: push | pop | peek | isEmpty.

The most intuitive solution is popping all elements into an array, sorting the array and pushing all elements back to the stack.
from stack import *
def sort_stack(s):
    elements = []
    while not s.isEmpty():
        elements.append(s.pop())
    quick_sort(elements, 0, len(elements) - 1)
    for i in range(0, len(elements)):
        s.push(elements[i])
def quick_sort(arr, start, end):
    if start >= end:
        return
    pivot = choose_pivot(arr, start, end)
    pivot_value = arr[pivot]
    arr[pivot], arr[start] = arr[start], arr[pivot]
    pivot = start
    p = start + 1
    while p <= end:
        if arr[p] <= pivot_value:
            pivot = pivot + 1
            arr[p], arr[pivot] = arr[pivot], arr[p]
        p = p + 1
    arr[pivot], arr[start] = arr[start], arr[pivot]
    quick_sort(arr, start, pivot - 1)
    quick_sort(arr, pivot + 1, end)
def choose_pivot(arr, start, end):
    return start
# Test case
if __name__ == "__main__":
    s = stack()
    s.push(3)
    s.push(1)
    s.push(4)
    s.push(2)
    s.push(8)
    s.push(7)
    sort_stack(s)
    for i in range(0, 7):
        print s.pop()

However, it is so intuitive that I don't think it is the most reasonable solution. As expected, the solution on the answer page uses two stacks to achieve the goal.
from stack import *
def sort_stack(s):
    s2 = stack()
    while not s.isEmpty():
        tmp = s.pop()
        while (not s2.isEmpty()) and (s2.peek() <= tmp):
            s.push(s2.pop())
        s2.push(tmp)
    while not s2.isEmpty():
        s.push(s2.pop())

Although the solution above cannot beat mine on running time, it is an innovative method.

Here I want to summarize the mistakes that I prone to make when I am implementing quicksort:
1) The ending condition of recursion is "if start >= end", not "if start == end"
2) The initial position of pivot is array[start], not array[0]
3) The initial position of working pointer is (start + 1), not start
4) In quick_sort() (or partition()), never use array[0] and array[len(array)]. You should use array[start] and array[end].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值