快速排序-python实现

def partition(ar, s, e):
    x=ar[s]
    i=s
    j=e
    while(i<j):
        while i<j and ar[j]>=x:
            j-=1
        if i<j:
            ar[i],ar[j]=ar[j],ar[i]
            i+=1
        while i<j and ar[i]<=x:
            i+=1
        if i<j:
            ar[i],ar[j]=ar[j],ar[i]
            j-=1
    return j

def quicksort(ar,s,e):
    if s<e:
        q=partition(ar,s,e)
        quicksort(ar,s,q-1)
        quicksort(ar,q+1,e)

arr = [1, 4, 7, 1, 5, 5, 3, 85, 34, 75, 23, 75, 2, 0]
# arr=[49,38,65,97,76,13,27]
quicksort(arr,0,len(arr)-1)
print(arr)

猜你喜欢

转载自blog.csdn.net/qq_36387683/article/details/79924550