Python 冒泡排序和快速排序(公开代码)

Python 冒泡排序和快速排序(公开代码),第1张

Python 冒泡排序
def bubble(n,list): #n默认为0
    for i in range(0,len(list)-n-1):
        temporary_variable=0#设置临时变量
        if list[i]>list[i+1]:
            temporary_variable=list[i + 1]
            list[i + 1]=list[i]
            list[i]=temporary_variable
    n=n+1
    if n!=len(list)-1:
        bubble(n, list)
    else:
        return list


list=[9,4,7,1,100,54,76,12,74,52,82,92,99,23,15]
bubble(0,list)
print(list)
Python 快速排序
def qulickly_sort(list,left,right):

    def sort_mid(list,left,right):
        temporary_variable = list[left]
        while (right != left):
            while (list[right] >= temporary_variable) and right != left:
                right -= 1
            list[left] = list[right]
            while (list[left] <= temporary_variable) and right != left:
                left += 1
            list[right] = list[left]
        list[left] = temporary_variable
        return left

    if left<right: #进入迭代
        mid= sort_mid(list, left, right)
        qulickly_sort(list, left, mid - 1)
        qulickly_sort(list, mid + 1, right)

list=[9,4,7,1,100,54,76,12,74,52,82,92,99,23,15]
qulickly_sort(list,0,len(list) - 1)
print(list)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/718536.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存