Python_排序算法实现

这里用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 time
import copy
import random
import threading
import math
import 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,t
def 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,t
def 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,t
def 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,t
def 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,t


def 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,t


def heapq_sort(L):
    #堆排序,使用内部堆的定义,就不自己自定义啦
    LL=copy.deepcopy(L)
    t1=time.time()
    heapq.heapify(LL)
    t2=time.time()
    t=t2-t1
    return LL,t

if __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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值