首页 > 代码库 > 用Python实现几种排序算法

用Python实现几种排序算法

#coding=utf-8# 1 快速排序算法def qksort(list):    if len(list)<=1:        return list    else:        pivot = list[0]        less=[x for x in list[1:] if x<pivot]        greater=[y for y in list[1:] if y>=pivot]        return qksort(less)+[pivot]+qksort(greater)    q=[1,3,8,3,2,6,5,3,5,7]t=qksort(q)print (t)#coding=utf-8# 2 插入排序算法def insection_sort(list):    n=len(list)    for index in range(1,n):        j=index-1        value=list[index]        while j>=0:            if value<list[j]:                list[j+1]=list[j]                list[j]=value            else :                break            j=j-1    return listq=[1,3,8,3,2,6,5,3,5,7]t=insection_sort(q)print (t)  #coding=utf-8# 3 冒泡排序算法def bubble_sort(list):    n=len(list)    for i in range (n):        for j in range(i+1,n):            if list[j]<list[i]:                temp=list[j]                list[j]=list[i]                list[i]=temp    return listq=[1,3,8,3,2,6,5,3,5,7]t=bubble_sort(q)print (t)  

 

用Python实现几种排序算法