python--快速排序实现

对于一个想找工作的人来说,快排必须能手写出来。

快排原理无需解释,直接给出python代码:

def quicksort(num,low,high):
    if low<high:
        location=partition(num,low,high)
        quicksort(num,low,location-1)
        quicksort(num,location+1,high)


def partition(num,low,high):
    pivot=num[low]
    while(low<high):
        while(low<high and num[high]>pivot):
            high-=1
        while (low < high and num[low] < pivot):
            low += 1
        temp=num[low]
        num[low]=num[high]
        num[high]=temp
    num[low]=pivot
    return low
pai=[5,12,79,2,4,8,6]
quicksort(pai,0,len(pai)-1)
print(pai)

猜你喜欢

转载自blog.csdn.net/wenqiwenqi123/article/details/81586120