排序算法运行效率问题

十大经典排序算法(动图演示)
各个算法的时间、空间复杂度如下图所示:
排序算法复杂度

在此博主用python对其中六大排序算法进行编程,源码和运行结果如下(如有问题,欢迎在评论区批评指出):

# -*- coding: utf-8 -*-
"""
Created on Sun Apr 19 23:18:40 2020

@author: 44886
"""
import copy
import time
import random
total_exchange = 0 #交换赋值次数
total_compare  = 0 #比较次数
def add_count(n_exchange,n_compare):
    global total_exchange,total_compare
    total_exchange+=n_exchange
    total_compare +=n_compare
    
def reset_count():
    global total_exchange,total_compare
    total_exchange=0
    total_compare =0
    
def print_count():
    global total_exchange,total_compare
    print("该排序算法赋值次数为:%d; 比较次数为:%d"%(total_exchange,total_compare))
    print()

def Bubble_Sort(aa):
    a = copy.deepcopy(aa)
    n = len(a)
    for i in range(n-1,-1,-1):
        for j in range(0,i,1):
            add_count(0,1)
            if a[j+1]<a[j]:
                temp = a[j]
                a[j]=a[j+1]
                a[j+1]=temp
                add_count(3,0)
    return a

def selection_sort(aa):
    a = copy.deepcopy(aa)
    n=len(a)
    for i in range(0,n-1,1):
        min=a[i]
        min_index=i    
        add_count(2,0)
        for j in range(i+1,n,1):
            add_count(0,1)
            if min>a[j]:
                min=a[j]
                min_index=j      
                add_count(2,0)                       
        temp=a[i]
        a[i]=min
        a[min_index]=temp
        add_count(3,0)
    return a

def Insertion_sort(aa):
    a=copy.deepcopy(aa)
    n=len(a)
    for i in range(1,n):
        for j in range(0,i):
            add_count(0,1)
            if a[j]>a[i]:
                a.insert(j,a[i])
                del a[i+1]
                add_count(2,0)
                break
    return a   


def Insertion_sort_self(aa):
    a = copy.deepcopy(aa)
    n=len(a)  
    add_count(1,0)
    for i in range(1,n):
        for j in range(0,i):
            add_count(0,1)
            if a[j]>a[i]:
                a.insert(j,a[i])
                del a[i+1]
                add_count(2,0)
                break
    return a
            

def Shell_Sort(aa,step):
    a=copy.deepcopy(aa)
    n=len(a)
    for s in range(step,0,-1):
        for i in range(0,s):
            a[i:n:s]=Insertion_sort_self(a[i:n:s])
    return a   


def merge_sort(aa):
    def merge(aa,bb):
        global total_exchange,total_compare
        a=copy.deepcopy(aa)
        b=copy.deepcopy(bb)
        merged_list=[]
        while True:
            if len(a)==0:
                merged_list = merged_list+b
                add_count(1,0)
                break
            elif len(b)==0:
                merged_list = merged_list+a
                add_count(1,0)
                break
            elif a[0]<b[0]:
                merged_list.append(a[0])
                add_count(1,0)
                del a[0]
            else :
                merged_list.append(b[0])
                add_count(1,0)
                del b[0]
            add_count(0,2)
        return merged_list
    global total_exchange,total_compare
    a=copy.deepcopy(aa)
    add_count(0,1)
    if len(a)<2:
        return a
    middle_position = int(len(a)/2)
    add_count(2,0)
    return merge(merge_sort(a[0:middle_position]),merge_sort(a[middle_position:len(a)]))        

def quick_sort(aa):
    a=copy.deepcopy(aa)
    n=len(a)
    if n<2:
        return a
    flag = 0
    for i in range(1,n):
        add_count(0,1)
        if(a[i]<a[i-1]):
            flag=1
            break
    if flag ==0:
        return a
    else:
        less=[]
        more=[]
        pivot=[]
        pivot.append(a[0])
        for i in range(1,n):
            add_count(1,1)
            if a[i]<pivot[0]:
                less.append(a[i])
            else:
                more.append(a[i])
        return quick_sort(less)+list(pivot)+quick_sort(more)
                
            
        
        

a = [random.random()*5000 for _ in range(20000)]

reset_count()
start_time=time.time()
Bubble_Sort(a)
end_time=time.time()
print("冒泡排序消耗时间为:%fs;"%(end_time-start_time))
print_count()

reset_count()
start_time=time.time()
selection_sort(a)
end_time=time.time()
print("选择排序消耗时间为:%fs;"%(end_time-start_time))
print_count()

reset_count()
start_time=time.time()
Insertion_sort(a)
end_time=time.time()
print("插入排序消耗时间为:%fs;"%(end_time-start_time))
print_count()

reset_count()
start_time=time.time()
Shell_Sort(a,10)
end_time=time.time()
print("希尔排序(子序列间隔为10)消耗时间为:%fs;"%(end_time-start_time))
print_count()

reset_count()
start_time=time.time()
Shell_Sort(a,5)
end_time=time.time()
print("希尔排序(子序列间隔为5)消耗时间为:%fs; "%(end_time-start_time))
print_count()

reset_count()
start_time=time.time()
merge_sort(a)
end_time=time.time()
print("归并排序消耗时间为:%fs; "%(end_time-start_time))
print_count()

reset_count()
start_time=time.time()
quick_sort(a)
end_time=time.time()
print("快速排序消耗时间为:%fs;"%(end_time-start_time))
print_count()
冒泡排序消耗时间为:96.852896s;
该排序算法赋值次数为:301508508; 比较次数为:199990000

选择排序消耗时间为:51.273974s;
该排序算法赋值次数为:442381; 比较次数为:199990000

插入排序消耗时间为:26.517787s;
该排序算法赋值次数为:39982; 比较次数为:99507155

希尔排序(子序列间隔为10)消耗时间为:156.139227s;
该排序算法赋值次数为:153793; 比较次数为:575526694

希尔排序(子序列间隔为5)消耗时间为:118.061816s; 
该排序算法赋值次数为:99119; 比较次数为:436355993

归并排序消耗时间为:0.614854s; 
该排序算法赋值次数为:320960; 比较次数为:561925

快速排序消耗时间为:0.315876s;
该排序算法赋值次数为:327344; 比较次数为:346871

C++ sort源码解析
C++ STL::Sort排序算法类似于快排,在快排的基础上,当递归深度超过一定深度(深度为排序元素数量的对数值)时转为堆排序,也有说用到插入排序。

原创文章 13 获赞 7 访问量 1290

猜你喜欢

转载自blog.csdn.net/weixin_43827285/article/details/105747158