quicks中的配分函数

2024-04-25 21:48:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中输入了一个快速排序,但是找不出为什么这个分区函数不能工作。你知道吗

def partition(arr,low, high):
    i=low
    for counter in range(low, high):
        if arr[counter]<arr[i]:
            s=arr[counter]
            del arr[counter]
            arr=[s,]+arr
            i+=1
        print(arr,i,counter)
    return i

arr=[10,11,6,3,5,7,8]
n=len(arr)
partition(arr,0,n)
print(arr)

输出:

[10, 11, 6, 3, 5, 7, 8] 0 0
[10, 11, 6, 3, 5, 7, 8] 0 1
[6, 10, 11, 3, 5, 7, 8] 1 2
[3, 6, 10, 11, 5, 7, 8] 2 3
[5, 3, 6, 10, 11, 7, 8] 3 4
[7, 5, 3, 6, 10, 11, 8] 4 5
[8, 7, 5, 3, 6, 10, 11] 5 6
[10, 11, 3, 5, 7, 8]

Tags: 函数inforif排序defcounterrange

热门问题