大江东去,浪淘尽,千古风流人物。故垒西边,人道是,三国周郎赤壁。乱石穿空,惊涛拍岸,卷起千堆雪。江山如画,一时多少豪杰。遥想公瑾当年,小乔初嫁了,雄姿英发。羽扇纶巾,谈笑间,樯橹灰飞烟灭。故国神游,多情应笑我,早生华发。人生如梦,一尊还酹江月。

南鱼

果然,我没有抓住重点

导航

python 经典排序方法(冒泡排序,快速排序,二分法查找)

闲来无事,回忆了一下刚开始学python的时候学的几个排序方法:

"""冒泡排序"""
num = [11, 3, 5, 6, -9, -8, -5, -66, 99, 88, 52]
for x in range(len(num)):
    for y in range(len(num)-x-1):
        if num[y] > num[y+1]:
            num[y], num[y+1] = num[y+1], num[y]
print(num)

for x in range(1, len(num)):
    for y in range(len(num)-x):
        if num[y] < num[y+1]:
            num[y], num[y+1] = num[y+1], num[y]
print(num)


"""快速排序"""
def quick_sort(lst):
    if len(lst)==1:
        return lst
    left = []
    right = []
    for x in range(1,len(lst)):
        if lst[0] > lst[x]:
            left.append(lst[x])
        elif lst[0] < lst[x]:
            right.append(lst[x])
        if len(left) > 1:
            left = quick_sort(left)
        elif len(right) > 1:
            right = quick_sort(right)
    return left+[lst[0]]+right


print(quick_sort(num))


# 二分法查找
def mid_search(lis,val):
    low = 0
    high = len(lis)-1
    while low <= high:
        mid = (low+high)//2
        if lis[mid] == val:
            return mid
        elif lis[mid] < val:
            low = mid+1
        else:
            high = mid-1
            
            
print(mid_search(range(0, 100000, 2), 35300))

嗯,还是原来的配方,还是熟悉的味道。。。。这些东西感觉好久都未用过了,,,,,,,,,,,,,,,,,,,,,,,,,

posted on 2018-11-21 11:47  南鱼羁荒渡  阅读(1085)  评论(4编辑  收藏  举报