python快速选择排序

import random


def input1(data, size):
    for i in range(size):
        data[i] = random.randint(1, 100)


def show(data, size):
    for i in range(size):
        print('%3d' % data[i], end='')
    print()


def quick(d, size, l, r):
    #  数据,长度,左,右
    if l < r:
        l_i = l + 1
        while d[l_i] < d[l]:
            if l_i + 1 > size:
                break
            l_i += 1
        r_i = r
        while d[r_i] > d[l]:
            r_i -= 1
        while l_i < r_i:
            d[l_i], d[r_i] = d[r_i], d[l_i]
            l_i += 1
            while d[l_i] < d[l]:
                l_i += 1
            r_i -= 1
            while d[r_i] > d[l]:
                r_i -= 1
        d[l], d[r_i] = d[r_i], d[l]
        for i in range(size):
            print('%3d' % d[i], end='')
        print()
        quick(d, size, l, r_i - 1)
        quick(d, size, r_i + 1, r)


def main():
    data = [0] * 100
    size = int(input('\n请输入数据'))
    input1(data, size)
    print('刚刚输入的数据')
    show(data, size)
    print('排序过程')
    quick(data, size, 0, size - 1)
    print('结果')
    show(data, size)


main()
上一篇:Quick Sort


下一篇:我也写个快速排序