91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python實現排序算法2

發布時間:2020-05-19 10:13:27 來源:網絡 閱讀:205 作者:小嘉欣 欄目:編程語言

4.希爾排序

# -*- coding:utf-8 -*-
def shell_sort(alist):
    """希爾排序"""
    n = len(alist)
    gap = n // 2

    while gap >= 1:
        for j in range(gap,n):
            i = j
            while i > 0:
                if alist[i] < alist[i-gap]:
                    alist[i],alist[i-gap] = alist[i-gap],alist[i]
                    i -=gap
                else:
                    break
        gap //= 2

if __name__ == "__main__":
    a = [6,86,3,5,0,43,90,100]
    print(a)
    shell_sort(a)
    print(a)

# [6, 86, 3, 5, 0, 43, 90, 100]
# [0, 3, 5, 6, 43, 86, 90, 100]

5.快速排序

# -*- coding:utf-8 -*-
def quick_sort(alist,first,last):
    """快速排序"""
    if first >= last:
        return
    # n = len(alist)
    mid_value = alist[first]
    low = first
    high = last

    while low < high:
        #High左移
        while low <high and alist[high] >= mid_value:
            high -= 1
        alist[low] = alist[high]

        # Low右移
        while low <high and alist[low] < mid_value:
            low += 1
        alist[high]=alist[low]

    alist[low]=mid_value
    quick_sort(alist,first,low-1)
    quick_sort(alist,low+1, last)

if __name__ == "__main__":
    a = [6,86,3,5,0,43,90,100]
    print(a)
    quick_sort(a,0,len(a)-1)
    print(a)

# [6, 86, 3, 5, 0, 43, 90, 100]
# [0, 3, 5, 6, 43, 86, 90, 100]

6.歸并排序

# -*- coding:utf-8 -*-
def merge_sort(alist):
    """歸并排序"""
    n = len(alist)
    if n <= 1:
        return alist
    mid =n // 2

    left_alist = merge_sort(alist[:mid])
    right_alist = merge_sort(alist[mid:])

    left_pointer,right_pointer = 0,0
    result = []

    while left_pointer < len(left_alist) and right_pointer < len(right_alist):
        if left_alist[left_pointer] < right_alist[right_pointer]:
            result.append(left_alist[left_pointer])
            left_pointer += 1
        else:
            result.append(right_alist[right_pointer])
            right_pointer += 1
    result += left_alist[left_pointer:]
    result += right_alist[right_pointer:]
    return  result

if __name__ == "__main__":
    a = [6, 86, 3, 5, 0, 43, 90, 10]
    print(a)
    sorted_list = merge_sort(a)
    print(sorted_list)

# [6, 86, 3, 5, 0, 43, 90, 10]
# [0, 3, 5, 6, 10, 43, 86, 90]
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

内乡县| 甘洛县| 苍南县| 泾源县| 长岛县| 凌源市| 彰化市| 台安县| 沽源县| 桂东县| 板桥市| 胶南市| 江油市| 固安县| 新郑市| 大荔县| 南涧| 达拉特旗| 凤庆县| 江安县| 泾阳县| 龙里县| 丘北县| 贡觉县| 岑巩县| 宁明县| 平阴县| 公安县| 伊春市| 浑源县| 昭通市| 资兴市| 保德县| 工布江达县| 修武县| 珲春市| 多伦县| 涟源市| 于田县| 利川市| 仁布县|