说是最快,但也感觉最难。。:(

来个总结。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

 python---快速排序 随笔

# coding = utf-8


# 快速排序
def partition(a_list, first, last):
    loop_count = 0
    pivot_value = a_list[first]
    left_mark = first + 1
    right_mark = last

    done = False
    while not done:
        loop_count += 1
        while left_mark <= right_mark and a_list[left_mark] <= pivot_value:
            loop_count += 1
            left_mark += 1
        while a_list[right_mark] >= pivot_value and right_mark >= left_mark:
            loop_count += 1
            right_mark -= 1

        if right_mark < left_mark:
            done = True
        else:
            temp = a_list[left_mark]
            a_list[left_mark] = a_list[right_mark]
            a_list[right_mark] = temp
    temp = a_list[first]
    a_list[first] = a_list[right_mark]
    a_list[right_mark] = temp

    print('======== quick_sort loop_count========', loop_count)

    return right_mark


def quick_sort_helper(a_list, first, last):
    loop_count = 0
    if first < last:
        loop_count += 1
        split_point = partition(a_list, first, last)
        quick_sort_helper(a_list, first, split_point-1)
        quick_sort_helper(a_list, split_point+1, last)
    print('======== quick_sort loop_count========', loop_count)


def quick_sort(a_list):
    quick_sort_helper(a_list, 0, len(a_list)-1)


my_list = [4, 5, 7, 2, 9, 7, 9, 54, 765, 45, 9876, 34, 12343, 36]
quick_sort(my_list)
print(my_list)

  

C:\Users\Sahara\.virtualenvs\test\Scripts\python.exe C:/Users/Sahara/PycharmProjects/test/python_search.py
======== quick_sort loop_count======== 15
======== quick_sort loop_count======== 0
======== quick_sort loop_count======== 12
======== quick_sort loop_count======== 0
======== quick_sort loop_count======== 10
======== quick_sort loop_count======== 2
======== quick_sort loop_count======== 0
======== quick_sort loop_count======== 0
======== quick_sort loop_count======== 1
======== quick_sort loop_count======== 9
======== quick_sort loop_count======== 3
======== quick_sort loop_count======== 0
======== quick_sort loop_count======== 2
======== quick_sort loop_count======== 0
======== quick_sort loop_count======== 0
======== quick_sort loop_count======== 1
======== quick_sort loop_count======== 1
======== quick_sort loop_count======== 4
======== quick_sort loop_count======== 0
======== quick_sort loop_count======== 0
======== quick_sort loop_count======== 1
======== quick_sort loop_count======== 1
======== quick_sort loop_count======== 1
======== quick_sort loop_count======== 1
======== quick_sort loop_count======== 1
[2, 4, 5, 7, 7, 9, 9, 34, 36, 45, 54, 765, 9876, 12343]

Process finished with exit code 0

  

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄