python3实现各种排序方法

# coding=gbk
import random
from array import array

def swap(lyst,i,j):
    temp = lyst[i]
    lyst[i] = lyst[j]
    lyst[j] = temp

#选择排序,复杂度O(n^2)
def selectionSort(lyst): 
    i = 0
    while i < len(lyst) - 1:
        minIndex = i
        j = i + 1
        while j < len(lyst):
            if lyst[j] < lyst[minIndex]:
                minIndex = j
            j += 1
        if minIndex != i:
            swap(lyst,minIndex,i)
        i += 1
#冒泡排序,复杂的O(n^2)
def bubbleSort(lyst):
    n = len(lyst)
    while n > 1:
        i = 1
        while i < n:
            if lyst[i] < lyst[i-1]:
                swap(lyst,i,i-1)
            i += 1
        n -= 1
#冒泡排序优化改进最好情况
def bubbleSortWithTweak(lyst):
    n = len(lyst)
    while n > 1:
        swapped = False
        i = 1
        while i < n:
            if lyst[i] < lyst[i-1]:
                swap(lyst,i,i-1)
                swapped = True
            i += 1
        if not swapped: return
        n -= 1
#插入排序,复杂的O(n^2)
def insertionSort(lyst):
    i = 1
    while i < len(lyst):
        itemToInsert = lyst[i]
        j = i - 1
        while j >= 0:
            if itemToInsert < lyst[j]:
                lyst[j+1] = lyst[j]
                j -= 1
            else:
                break
        lyst[j+1] = itemToInsert
        i += 1
#快速排序,最好情况,复杂的O(n*(log2 n)),最坏情况,复杂的O(n^2)
def quicksort(lyst):
    quicksortHelper(lyst,0,len(lyst)-1)

def quicksortHelper(lyst,left,right):
    if left < right:
        pivotLocation = partition(lyst,left,right)
        quicksortHelper(lyst,left,pivotLocation-1)
        quicksortHelper(lyst,pivotLocation+1,right)
def partition(lyst,left,right):
    middle = (left+right) // 2
    pivot = lyst[middle]
    lyst[middle] = lyst[right]
    lyst[right] = pivot
    boundary = left
    for index in range(left,right):
        if lyst[index] < pivot:
            swap(lyst,index,boundary)
            boundary += 1
    swap(lyst,right,boundary)
    return boundary

#合并排序
def mergeSort(lyst):
    copyBuffer = [0]*(len(lyst))
    mergeSortHelper(lyst,copyBuffer,0,len(lyst)-1)

def mergeSortHelper(lyst,copyBuffer,low,high):
    if low < high:
        middle = (low+high)//2
        mergeSortHelper(lyst,copyBuffer,low,middle)
        mergeSortHelper(lyst,copyBuffer,middle+1,high)
        merge(lyst,copyBuffer,low,middle,high)

def merge(lyst,copyBuffer,low,middle,high):
    i1 = low
    i2 = middle + 1
    for i in range(low,high+1):
        if i1 > middle:
            copyBuffer[i] = lyst[i2]
            i2 += 1
        elif i2 > high:
            copyBuffer[i] = lyst[i1]
            i1 += 1      
        elif lyst[i1] < lyst[i2]:
            copyBuffer[i] = lyst[i1]
            i1 += 1      
        else :
            copyBuffer[i] = lyst[i2]
            i2 += 1 
    for i in range(low,high+1):
        lyst[i] = copyBuffer[i]

def main(size = 20,sort = mergeSort):   
    lyst = []
    for count in range(size):
        lyst.append(random.randint(1,size+1))
    print(lyst)
    sort(lyst)
    print(lyst)

if __name__ == "__main__":
    main()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值