排序算法之快速排序

#-*-coding:utf-8 -*-

import random

def quick_sorted(alist,first,last):
    if first >= last:
        return
    else:
        left_pointer = first
        right_pointer = last
        mid_value = alist[first]
        while left_pointer < right_pointer:
            while left_pointer < right_pointer and alist[right_pointer] >= mid_value:
                right_pointer -= 1
            alist[left_pointer] = alist[right_pointer]
            while left_pointer < right_pointer and alist[left_pointer] <= mid_value:
                left_pointer += 1
            alist[right_pointer] = alist[left_pointer]
        alist[left_pointer] = mid_value
        quick_sorted(alist,first,left_pointer-1)
        quick_sorted(alist,left_pointer+1,last)

if __name__ == '__main__':
    alist = [random.randint(1,100) for _ in range(30)]
    print(alist)
    quick_sorted(alist,0,len(alist)-1)
    print(alist)

使用Python写的快排,测试数据是30个1到100之间的整数列表。

上一篇:我的第十九篇博客---JavaScript基本操作


下一篇:二分差找