python十大排序算法

python写的一些排序算法

import math
#1.冒泡排序
def bubbleSort(list):
    for i in range(1,len(list)):
        for j in range(0,len(list)-i):
            if(list[j]>list[j+1]):
                list[j],list[j+1]=list[j+1],list[j]
    return list

#2.选择排序
def selectionSort(list):
    for i in range(len(list)-1):
        minindex=i
        for j in range(i+1,len(list)):
            if list[j]<list[minindex]:
                minindex=j
        if i!=minindex:
            list[i],list[minindex]=list[minindex],list[i]
    return list


#3.插入排序
def insertionSort(list):
    for i in range(len(list)):
        preindex=i-1
        current=list[i]
        while preindex>=0 and list[preindex]>current:
            list[preindex+1]=list[preindex]
            preindex-=1
        list[preindex+1]=current
    return list

#4.希尔排序
def shellSort(list):
    gap=1
    while(gap<len(list)/3):
        gap=gap*3+1
    while gap>0:
        for i in range(gap,len(list)):
            temp=list[i]
            j=i-gap
            while j>=0 and list[j]>temp:
                list[j+gap]=list[j]
                j-=gap
            list[j+gap]=temp
        gap=math.floor(gap/3)
    return list


#5.归并排序
def mergeSort(list):
    if(len(list)<2):
        return list
    result=[]
    middle=math.floor(len(list)/2)
    left,right=list[0:middle],list[middle:]
    while len(left) and len(right):
        if(left[0]<=right[0]):
            result.append(left.pop(0))
        else:
            result.append(right.pop(0))
    while left:
        result.append(left.pop(0))
    while right:
        result.append(right.pop(0))
    return result


#6.快速排序
def quickSort(list,left=None,right=None):
    left=0 if not isinstance(left,(int,float)) else left
    right=len(list)-1 if not isinstance(right,(int,float)) else right
    if(left<right):
        partitionindex=partition(list,left,right)
        quickSort(list,left,partitionindex-1)
        quickSort(list,partitionindex+1,right)
    return list


def partition(list,left,right):
    pivot=left
    index=pivot+1
    i=index
    while i<=right:
        if(list[i]<list[pivot]):
            list[i],list[index]=list[index],list[i]
            index+=1
        i+=1
    list[pivot],list[index-1]=list[index-1],list[pivot]
    return index-1

#7.堆排序
def buildmaxHeap(list):
    for i in range(math.floor(len(list)/2),-1,-1):
        heapify(list,i)

def heapify(list,i):
    left=2*i+1
    right=2*i+2
    largest=i
    if(left<list_len and list[left]>list[largest]):
        largest=left
    if(right<list_len and list[right]>list[largest]):
        largest=right
    if(largest!=i):
        list[i],list[largest]=list[largest],list[i]
        heapify(list,largest)

def heapSort(list):
    global list_len
    list_len=len(list)
    buildmaxHeap(list)
    for i in range(len(list)-1,0,-1):
        list[0],list[i]=list[i],list[0]
        list_len-=1
        heapify(list,0)
    return list


#8.计数排序
def countingSort(list):
    bucketlen=max(list)+1
    bucket=[0]*bucketlen
    sortedindex=0
    for i in range(len(list)):
        if not bucket[list[i]]:
            bucket[list[i]]=0
        bucket[list[i]]+=1
    for j in range(bucketlen):
        while(bucket[j]>0):
            list[sortedindex]=j
            sortedindex+=1
            bucket[j]-=1
    return list


#9.桶排序
def bucketSort(list):
    minnum=min(list)
    maxnum=max(list)
    bucketnum=(maxnum-minnum)/len(list)
    count_list=[[] for i in range(len(list)+1)]
    for i in list:
        count_list[int((i-minnum)//bucketnum)].append(i)
    list.clear()
    for i in count_list:
        for j in sorted(i):
            list.append(j)
    return list


#10.基数排序
def radixSort(list):
    i=0
    n=1
    maxnum=max(list)
    while(maxnum>10**n):
        n+=1
    while(i<n):
        bucket={
     }
        for x in range(10):
            bucket.setdefault(x,[])
        for x in list:
            radix=int((x/(10**x))%10)
            bucket[radix].append(x)
        j=0
        for k in range(10):
            if(len(bucket[k])!=0):
                for y in bucket[k]:
                    list[j]=y
                    j+=1
        i+=1
    return list



list=[0, 3, 11, 5, 98, 1000, 20]
print("排序前的列表为:",list)
print("冒泡排序结果为:",bubbleSort(list))
print("选择排序结果为:",selectionSort(list))
print("插入排序结果为:",insertionSort(list))
print("希尔排序结果为:",shellSort(list))
print("归并排序结果为:",mergeSort(list))
print("快速排序结果为:",quickSort(list))
print("堆排序结果为:",heapSort(list))
print("计数排序结果为:",countingSort(list))
print("桶排序结果为:",bucketSort(list))
print("基数排序结果为:",radixSort(list))



运行结果:
python十大排序算法_第1张图片

你可能感兴趣的:(python)