五大排序算法Python版本代码及其总结

后续会尽量完善:2018-08-01

稳定排序:是指当有一个相等的数字进来时,它会确定性的放在其相等数字的后面

1冒泡排序

时间复杂度:最好状态O(n),最差O(n2),空间复杂度O(1),属于稳定排序

# -*- coding: utf-8 -*-
"""
Created on Wed Aug  1 08:36:54 2018

@author: zhipeng

function:冒泡排序

input:list
"""

def Bubblle_sort(data):
    for  i in range(0,len(data)):
        for j in range(0,len(data)-1-i):
            if data[j]>data[j+1]:
                tmp = data[j+1]
                data[j+1] = data[j]
                data[j] = tmp
    print(data)
    
data = [1,5,2,3,9,6]
Bubblle_sort(data)

打印内容:[1, 2, 3, 5, 6, 9]
更进一步,上面是顺序排列,如果需要倒序排列的话,data[j]<data[j+1],输出是[9,6,5,3,2,1]

2选择排序:

时间复杂度:最好=最差O(n2), 空间复杂度O(1), 稳定性:不稳定

# -*- coding: utf-8 -*-
"""
Created on Wed Aug  1 08:36:54 2018

@author: zhipeng

function:选择排序

input:list
"""

def select_sort(data):
    for i in range(0,len(data)-1):
        min_index = i
        for j in range(i+1,len(data)):
            if data[min_index]>data[j]:
                min_index=j
                
        tmp = data[min_index]
        data[min_index] = data[i]
        data[i] = tmp
    
    print(data)
    
data = [1,5,2,3,9,6]
select_sort(data)
输出结果:[1,2,3,5,6,9]
同理,要是想倒序输出,改变一下if条件,就可以了。

3插入排序

复杂度:最好O(n),最差O(n2),空间复杂度O(1) ,稳定性:不稳定

# -*- coding: utf-8 -*-
"""
Created on Wed Aug  1 08:36:54 2018

@author: zhipeng

function:插入排序

input:list
"""

def insert_sort(data):
    for i in range(1,len(data)):
        pre_index = i - 1
        current = data[i]
        while pre_index>=0 and data[pre_index] > data[i]:
            data[pre_index + 1] = data[pre_index]
            pre_index -=1
        data[pre_index +1] = current
        
    print(data)
    
data = [1,5,2,3,9,6]
insert_sort(data)
打印内容:[1,2,3,5,6,9]

4归并排序

时间复杂度:最好=最坏O(nlogn),空间复杂度O(n),稳定性:不稳定

# -*- coding: utf-8 -*-
"""
Created on Wed Aug  1 08:36:54 2018

@author: zhipeng

function:归并排序

input:list
"""

def merge_sort(data):
    if len(data) == 1:
        return data
    
    mid =  int(len(data)/2)
    left = data[:mid]
    right = data[mid:]
    
    left_merge = merge_sort(left)
    right_merge = merge_sort(right)
    return merge(left_merge,right_merge)

def merge(left,right):
    result = []
    while len(left)>0 and len(right)>0:
        if left[0] <= right[0]:
            result.append(left.pop(0))
            
        else:
            result.append(right.pop(0))
            
    result += left
    result += right
    return result
    
data = [1,5,2,3,9,6]
print(merge_sort(data))
打印内容:[1,2,3,5,6,9]

5快速排序

时间复杂度:最好O(nlogn),最坏O(n2),稳定性,不稳定

# -*- coding: utf-8 -*-
"""
Created on Tue Jul 31 21:05:57 2018

@author: zhip_hu

function:quick_sort

input:list
"""

def quick_sort(arr,first_index,last_index):
    if first_index < last_index:
        divi_index = partition(arr,first_index,last_index)
        
        quick_sort(arr,first_index,divi_index)
        quick_sort(arr,divi_index+1,last_index)
        return arr
    else:
        return arr
    

def dive_index(arr,first_index,last_index):
    i = first_index -1
    for j in range(first_index,last_index):
        if arr[j]<= arr[last_index]:
            i +=1
            arr[i],arr[j] = arr[j],arr[i]
            
    arr[i+1],arr[last_index] = arr[last_index],arr[i+1]
    return i

arr = [1,5,3,2,88,7]
first_index = 0
last_index = len(arr) -1 
print(quick_sort(arr,first_index,last_index))
打印内容:[1,2,3,5,7,88]

 

    原文作者:五大常用算法
    原文地址: https://blog.csdn.net/zehui6202/article/details/81323829
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞