用Python实现常见的排序

冒泡排序

def bubble_sort(alist):
    n=len(alist)
    for j in range(n-1):
        for i in range(0,n-1-j):
            if alist[i]>alist[i+1]:
                alist[i],alist[i+1]=alist[i+1],alist[i]
if __name__=="__main__":
    li=[54,26,93,17,77,31,44,55]
    print(li)
    bubble_sort(li)
    print(li)

归并排序

import time
def merge_sort(alist):
    start=time.clock()
    n=len(alist)
    if(n<=1):
        return alist;
    mid=n//2
    left_list=merge_sort(alist[:mid])
    right_list=merge_sort(alist[mid:])
    left_pointer,right_pointer=0,0
    result=[]
    while(left_pointer

插入排序

def insert_sort(alist):
    n=len(alist)
    for j in range(1,n):
        i=j
        while i>0:
            if alist[i]

还有其他排序方法再更新

你可能感兴趣的:(用Python实现常见的排序)