欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python实现冒泡排序、选择排序和插入排序

程序员文章站 2022-06-11 19:20:12
...

冒泡排序

def bubSort(arr):
    length = len(arr)
    for i in range(length):
        for j in range(length-1-i):
            if arr[j] > arr[j+1]:
                temp = arr[j]
                arr[j] = arr[j+1]
                arr[j+1] = temp

选择排序

def selectSort(arr):
    length = len(arr)
    for outer in range(length-1):
        minner = outer
        for inner in range(outer,length):
            if arr[inner]<arr[minner]:
                minner = inner
        temp = arr[outer]
        arr[outer] = arr[minner]
        arr[minner] = temp

插入排序

def insertSort(arr):
    length = len(arr)
    for outer in range(1,length):
        inner = outer
        temp = arr[inner]
        while(inner>=1):
            if arr[inner-1] > temp:
                arr[inner] = arr[inner-1]
                inner -= 1
            else:
                break
        arr[inner] = temp
相关标签: 数据结构与算法