Python随笔-快排

def swap(arr, i, j):
    temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp

def part(arr, beg, end):
    if end - beg <= 0:
        return beg
    v = arr[beg]
    idx = beg + 1
    for x in range(beg+1, end+1):
        if(arr[x] < v):
            swap(arr, idx, x)
            idx += 1
    swap(arr, idx-1, beg)
    return idx-1


def sort(arr, beg, end):
    if end - beg <= 0:
        return
    i = part(arr, beg, end)
    sort(arr, beg, i)
    sort(arr, i+1, end)

arr = [5, 10, 6, 7, 1, 2, 3, 0, 3, 10, 99, 22, 33, 23, 45]
sort(arr, 0, len(arr)-1)
print(arr)

 

posted @ 2018-10-04 22:52  吴筱军  阅读(127)  评论(0编辑  收藏  举报