排序Python代码

排序Python代码

1.冒泡排序

def bubblesort(arr):
    for i in range(1,len(arr)):
        for j in range(0, len(arr) - i):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr

def BubbleSort(arr):
    if not arr or len(arr) < 2:
        return arr
    for i in range(len(arr)-1, 0, -1):
        for j in range(0, i):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr
def bubblesort(arr):
    flag = True
    i = 1
    while i <= len(arr)-1 and flag:
        flag = False
        for j in range(0, len(arr)-i):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
                flag = True
        i  += 1
    return arr

def BubbleSort(arr):
    if not arr or len(arr) < 2:
        return arr
    flag = True
    i = len(arr) - 1
    while i > 0 and flag: 
        flag = False
        for j in range(0, i):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
                flag = True
        i -= 1
    return arr

2.选择排序

def selectionsort(arr):
    for i in range(0, len(arr)-1):
        #记录最小数的索引
        minIndex = i 
        for j in range(i+1, len(arr)):
            if arr[j] < arr[minIndex]:
                minIndex = j
        # i不是最小数时,将i和最小数交换
        if i != minIndex:
            arr[i], arr[minIndex] = arr[minIndex], arr[i]
    return arr        

3.插入排序

def insertionSort(arr):
    if not arr or len(arr) < 2:
        return arr
    for i in range(1, len(arr)):
        j = i-1
        cur = arr[i]
        while j >= 0 and arr[j] > cur:
            arr[j+1] = arr[j]
            j -= 1
        arr[j+1] = cur
    return arr

def InsertionSort(arr):
    if not arr or len(arr) < 2:
        return arr
    for i in range(1, len(arr)):
        for j in range(i-1, -1, -1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr

4.希尔排序

def shellSort(arr):
    n = len(arr)
    gap = n//2

    while gap > 0:
        for i in range(gap, n):
            temp = arr[i]
            j = i
            while j >= gap and arr[j-gap] > temp:
                arr[j] = arr[j-gap]
                j -= gap
            arr[j] = temp
        gap = gap//2
    return arr

5.归并排序

def mergeSort(arr):
    if len(arr) < 2:
        return arr
    mid = len(arr)//2
    left, right = arr[0:mid], arr[mid:]
    return merge(mergeSort(left), mergeSort(right))

def merge(left, right):
    result = []
    while left and 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(arr, left=None, right=None):
    left = 0 if not isinstance(left,(int,float)) else left
    right = len(arr)-1 if not isinstance(right,(int,float)) else right
    if left < right:
        partitionIndex = partition(arr, left, right)
        quicksort(arr,left,partitionIndex-1)
        quicksort(arr,partitionIndex+1,right)
    return arr

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

def swap(arr,i, j):
    arr[i], arr[j] = arr[j], arr[i]


#利用荷兰国旗问题改进快排
def quicksort(arr, L, R):
    if L < R:
        p = partition(arr, L, R)
        quicksort(arr, L, p[0] - 1)
        quicksort(arr, p[1] + 1, R)

def partition(arr, L, R):
    less = L - 1
    more = R
    while L < more:
        if arr[L] < arr[R]:
            swap(arr, less+1, L)
            less += 1
            L += 1
        elif arr[L] > arr[R]:
            swap(arr, more-1, L)
            more -= 1
        else:
            L += 1
    swap(arr, more, R)
    return[less+1, more]

def swap(arr, i, j):
    arr[i], arr[j] = arr[j], arr[i]


    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值