python实现快速排序

#!/usr/bin/env python
# -*- coding: utf-8 -*-
def quick_sort(arr):
    l = len(arr)
    _quick_sort(arr, 0, l -1)
tmp = 0
def _quick_sort(arr, start, end):
    print  start,end
    if start >= end:
        return
    middle = partition(arr, start, end)
    _quick_sort(arr, start, middle - 1)
    _quick_sort(arr, middle + 1, end)

def partition(arr, start, end):
    i = (start+end)/2
    while(start < end):
        while(start < end and arr[start] <= arr[i]):
            start = start + 1
        while(start < end and arr[end] >= arr[i]):
            end = end - 1
        if start < end:
            arr[start], arr[end] = arr[end], arr[start]
    if start > i and  start - 1 != i :
        arr[start -1],arr[i] = arr[i], arr[start - 1]
        i = start -1
    elif start < i :
        arr[start], arr[i] = arr[i], arr[start]
        i = start
    return i

arr = [1,3,9,3,4,8,8,9]
quick_sort(arr)

  

posted @ 2017-10-18 14:17  天空中的蜂蜂  阅读(195)  评论(0编辑  收藏  举报