Python_排序算法实现

来源:互联网 发布:菜鸟网络航空港区地址 编辑:程序博客网 时间:2024/06/13 02:23

这里用Python给出几种常用排序算法:

  1. 插入排序
  2. 依次找最小值排序
  3. 冒泡排序
  4. Python列表内部排序(未知算法)
  5. 希尔排序
  6. 快速排序
  7. 堆排序
  8. 树排序

注意:

  • 使用copy模块,注意深复制与浅复制的区别
  • 这里代码尚未经过优化,速度方面有待提高,建议使用Python内置排序函数
# -*- coding: GBK -*-"""Created on Mon Oct 10 16:38:16 2016@author: zhangweiguo"""import timeimport copyimport randomimport threadingimport mathimport heapq'''插入排序依次找最小值排序冒泡排序系统内部排序****best希尔排序快速排序堆排序树排序'''def insert_sort(L):    #插入排序    t1=time.time()    n=len(L)    LL=[]    for i in xrange(n):        a=L[i]        LL.append(a)        for j in xrange(i):            if  a<=LL[j]:                b=LL[i]                LL[i]=LL[j]                LL[j]=b    t2=time.time()    t=t2-t1    return LL,tdef min_sort(L):    #直接选择排序:每次找最小值,然后更新原列表    t1=time.time()    LL=copy.deepcopy(L)    LLL=[]    n=len(L)    for i in xrange(n):        m=min(LL)        LL.pop(LL.index(m))        LLL.append(m)    t2=time.time()    t=t2-t1    return LLL,tdef bubble_sort(L):    #冒泡排序    t1=time.time()    LL=copy.deepcopy(L)    n=len(LL)    for i in xrange(n):        for j in xrange(i,n):            a=LL[i]            b=LL[j]            if a>b:                LL[i]=b                LL[j]=a    t2=time.time()    t=t2-t1    return LL,t        def system_sort(L):    #系统内部自定义sort函数    t1=time.time()    LL=copy.deepcopy(L)    LL.sort()    t2=time.time()    t=t2-t1    return LL,tdef hill_sort(L):    #希尔排序    t1=time.time()    LL=copy.deepcopy(L)    n=len(L)    d=math.floor(n/2)    d=int(d)    while d>=1:        for i in range(n-d):            LL[i:i+d+1].sort()        d=d/2    t2=time.time()    t=t2-t1    return LL,tdef fast_sort(L):    #快速排序    def fast_sort_one(LL,LLL,low):        n=len(LL)        if n==1:            LLL[low]=LL[0]            return        if n==0:            return        s=LL[0];L1=[];L2=[]        for i in xrange(1,n):            if LL[i]>s:                L2.append(LL[i])            else:                L1.append(LL[i])        n1 = len(L1);n2 = len(L2)        LLL[n1+low]=s        left=low        right=low+n1+1        fast_sort_one(L1,LLL,left)        fast_sort_one(L2,LLL,right)    t1=time.time()    LL = copy.deepcopy(L)    LLL = copy.deepcopy(L)    fast_sort_one(LL,LLL,0)    t2=time.time()    t=t2-t1    return LLL,tdef tree_sort(L):    #树排序:两两比较,依次选出最小值    def find_min(LL):        L_copy = []        n = len(LL)        n2 = int(n / 2)        while len(LL) != 1:            if 2 * n2 != n:                L_copy.append(LL[n - 1])            for i in xrange(n2):                if LL[2 * i] > LL[2 * i + 1]:                    L_copy.append(LL[2 * i + 1])                else:                    L_copy.append(LL[2 * i])            LL = copy.deepcopy(L_copy)            L_copy = []            n = len(LL)            n2 = int(n / 2)        return LL[0]    n=len(L)    t1=time.time()    L_copy=copy.deepcopy(L)    LL=[]    for i in xrange(n):        s=find_min(L_copy)        LL.append(s)        L_copy.remove(s)    t2=time.time()    t=t2-t1    return LL,tdef heapq_sort(L):    #堆排序,使用内部堆的定义,就不自己自定义啦    LL=copy.deepcopy(L)    t1=time.time()    heapq.heapify(LL)    t2=time.time()    t=t2-t1    return LL,tif __name__=='__main__':    L=[]    for i in xrange(1000):        L.append(random.randint(1,1000))    L1,t=insert_sort(L)    print '插入排序时间:    ',t    L2,t=bubble_sort(L)    print '冒泡排序时间:    ',t    L3,t=min_sort(L)    print '直接选择排序时间:',t    L4,t=system_sort(L)    print '系统排序时间:    ',t    L5,t=hill_sort(L)    print '希尔排序时间:    ',t    L6,t = fast_sort(L)    print '快速排序时间:    ',t    L7,t = heapq_sort(L)    print '堆排序时间:      ',t    L8, t = heapq_sort(L)    print '二叉树排序时间    ', t
    #下面用了一个多线程模块,有兴趣可自行添加补全代码,查看效果    '''    tt1=time.time()    T=[]    T.append(threading.Thread(target=insert_sort,args=(L,)))    T.append(threading.Thread(target=bubble_sort,args=(L,)))    for i in T:        i.start()    T[0].join()    T[1].join()    tt2=time.time()    tt=tt2-tt1    print '多线程总时间:',tt    '''

结果:(1000个随机数进行排序)
插入排序时间:       0.0579998493195
冒泡排序时间:       0.0650000572205
直接选择排序时间:0.0220000743866
系统排序时间:       0.0019998550415
希尔排序时间:       0.133000135422
快速排序时间:       0.00600004196167
堆排序时间:           0.000999927520752
二叉树排序时间:    0.0

0 0
原创粉丝点击