quick sort python

#!/usr/bin/env python

def quick_sort(a_array, left, right):
    if len(a_array) == 0: return a_array
    if left < right:
        mid = partition(a_array, left, right)
        print(a_array)
        quick_sort(a_array, left, mid - 1)
        quick_sort(a_array, mid + 1, right)


def partition(a_array, left, right):
    tmp = a_array[left]
    while left < right:
        while left < right and a_array[right] >= tmp:
            right -= 1
        a_array[left] = a_array[right]
        while left < right and a_array[left] <= tmp:
            left += 1
        a_array[right] = a_array[left]
    a_array[left] = tmp
    return left

if __name__ == "__main__":
    arr = [ 3, 1, 2, 5, 7, 4, 6, 2, 9, 8]
    print(arr)
    quick_sort(arr, 0, len(arr)-1)
    print('after:%s' % arr)

你可能感兴趣的:(算法,Python,算法)