Python数据结构-排序算法

本文详细介绍了冒泡排序、选择排序、插入排序、希尔排序、快速排序和归并排序六种经典排序算法的原理及Python实现。每种排序算法都有其独特之处,适用于不同的数据场景。通过实例展示了它们的排序效果,帮助读者深入理解各种排序算法的效率和应用场景。
摘要由CSDN通过智能技术生成

冒泡排序:

def bubble_sort(alist):
    for i in range(len(alist)-1,0,-1):
        for j in range(i):
            if alist[j]>alist[j+1]:
                alist[j],alist[j+1] = alist[j+1],alist[j]

li = [54,26,93,17,77,31,44,55,20]
bubble_sort(li)
print(li)

结果:

[93, 77, 55, 54, 44, 31, 26, 20, 17]

选择排序:

def select_sort(alist):
    n=len(alist)
    for i in range(n-1):
        min_index = i
        for j in range(i+1,n):
            if alist[j] < alist[min_index]:
                min_index = j
        if min_index != i:
            alist[i],alist[min_index] = alist[min_index],alist[i]

li = [54,26,93,17,77,31,44,55,20]
select_sort(li)
print(li)

结果:

[17, 20, 26, 31, 44, 54, 55, 77, 93]

插入排序:

def insert_sort(alist):
    for i in range(1,len(alist)):
        for j in range(i,0,-1):
            if alist[j]<alist[j-1]:
                alist[j],alist[j-1] = alist[j-1],alist[j]

li = [54,26,93,17,77,31,44,55,20]
insert_sort(li)
print(li)

结果:

[17, 20, 26, 31, 44, 54, 55, 77, 93]

希尔排序:

def shell_sort(arry):
    n = len(arry)
    gap = n//2
    while gap>0:
        for i in range(gap,n):
            j = i
            while(j>=gap and arry[j-gap] > arry[j]):
                arry[j-gap],arry[j] = arry[j],arry[j-gap]
                j -= gap

        gap = gap//2

alist = [54,26,93,17,77,31,44,55,20,12]
shell_sort(alist)
print(alist)

[12, 17, 20, 26, 31, 44, 54, 55, 77, 93]

快速排序:

def quick_sort2(arry,start,end):
    if start>=end:
        return

    piv = arry[start]
    left = start
    right = end

    while left<right:
        while left<right and arry[right]>=piv:
            right -=1
        arry[left] = arry[right]

        while left<right and arry[left]<piv:
            left += 1
        arry[right] = arry[left]

    arry[right] = piv

    quick_sort2(arry,start,right-1)

    quick_sort2(arry,right+1,end)

li=[54,26,93,17,77,31,44,55,20]
quick_sort2(li,0,len(li)-1)
print(li)

[17, 20, 26, 31, 44, 54, 55, 77, 93]

归并排序:

def merge_sort(arry):
    if len(arry)<=1:
        return arry

    mid = len(arry)//2

    # 左侧递归调用归并排序到有序
    left_arry = merge_sort(arry[:mid])
    # 右侧递归调用归并排序到有序
    right_arry = merge_sort(arry[mid:])

    l , r = 0,0
    result =[]
    while l<len(left_arry) and r<len(right_arry):
        if left_arry[l]<right_arry[r]:
            result.append(left_arry[l])
            l+=1
        else:
            result.append(right_arry[r])
            r += 1

    result = result + left_arry[l:]
    result = result + right_arry[r:]

    return result

alist = [54,26,93,17,77,31,44,55,20,12]
new = merge_sort(alist)
print(new)

[12, 17, 20, 26, 31, 44, 54, 55, 77, 93]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值