递归不会在python中停止

时间:2018-11-24 19:22:07

标签: python recursion quicksort

我试图在python中递归地实现quicksort,但是遇到了一个问题。一旦快速排序的每次迭代的长度为1,此代码应停止递归,但永远不会重复。有人知道为什么吗?

#!/usr/bin/python
import fileinput
import numpy as np
import string

def Quicksort(arrayIn, start, end):
    pivot = end
    temp = 0

    if end <= start:
        return

    for e in range(1, len(arrayIn)):
        if arrayIn[e] > arrayIn[pivot]:
            temp = arrayIn[pivot]
            arrayIn[pivot] = arrayIn[e]
            arrayIn.pop(e)
            arrayIn.insert((pivot - 1), temp)

    Quicksort(arrayIn, 0, (pivot - 1))
    Quicksort(arrayIn, (pivot + 1), (len(arrayIn) - 1))

for line in fileinput.input():
    myList = line.split(",")

noSpace = [x.strip(' ') for x in myList]

myArray = []

for x in noSpace:
    myArray.append(x)

Quicksort(myArray, 0, (len(myArray) - 1))
print myArray

0 个答案:

没有答案