Python实现十种经典排序算法

1、冒泡排序

基本思想:一次只比较两个元素,算法的时间复杂度为 O ( n 2 ) O(n^2) O(n2)

def bub_Sort(alist):
    for passnum in range(len(alist)-1,0,-1):
        for i in range(passnum):
            if alist[i] > alist[i+1]:
                alist[i],alist[i+1] = alist[i+1],alist[i]

L1 = [54,34,67,32,98,7,58,43,42,41,45,65,64,53,90]
bub_Sort(L1)
print("L1:",L1)

2、插入排序

基本思想:将一个数据插入到已经排好序的数组中,算法的时间复杂度为 O ( n 2 ) O(n^2) O(n2)

def insert_Sort(alist):
    for index in range(1, len(alist)):
        #current用来暂存当前要插入的元素值
        current = alist[index]
        pos = index
        #把所有已经排好序的元素中比current大的向后挪
        while pos > 0 and alist[pos - 1] > current:
            alist[pos] = alist[pos - 1]
            pos = pos - 1
        alist[pos] = current


L1 = [4,7,5,1,2,3,6]
insert_Sort(L1)
print("L1:", L1)

3、选择排序

基本思想:每次选择最小的元素放到当前位置

def sel_Sort(alist):
    for  i in range(len(alist)-1,0,-1):
        pos_max = 0
        for j in range(1,i+1):
            if alist[j] > alist[pos_max]:
                pos_max = j
        alist[i],alist[pos_max] = alist[pos_max],alist[i]


L1 = [54,34,67,32,98,7,58,43,42,41,45,65,64,53,90]
sel_Sort(L1)
print("L1:",L1)

4、希尔排序

是插入排序的一种
其实希尔排序并不能保证排序之后所有元素的顺序,在希尔排序之后应该再用一边插入排序。最后一遍插入排序比较的项数很少,因为希尔排序已经把绝大部分排好了。

def shell_Sort(alist):
    gap = len(alist)//2
    while gap > 0:
        for start in range(gap):
            gapInsertsort(alist, start, gap)
        print("After increments of size", gap, "The list is", alist)
        gap = gap // 2

#这里步骤几乎与插入排序相同,默认前面是已经排好序的
def gapInsertsort(alist, start, gap):
    for i in range(start+gap, len(alist), gap):
        current = alist[i]
        position = i
        while position >= gap and alist[position - gap] > current:
            alist[position] = alist[position - gap]
            position = position - gap
        alist[position] = current


L1 = [54, 34, 67, 32, 98, 7, 58, 43, 42, 41, 45, 65, 64, 53, 90]
shell_Sort(L1)

5、归并排序

基本思想:分而治之的思想,先把大的数组分为两个小规模的数组,把两个小数组分别排序,再组合。算法的时间复杂度 O ( n l o g n ) O(nlogn) O(nlogn)

def merge_sort(lists):
    if len(lists) <=1:
        return lists
    mid = len(lists)//2
    left = merge_sort(lists[:mid])
    right = merge_sort(lists[mid:])
    return merge(left,right)
def mmerge(left,right):
    res = []
    i = j = 0
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            res.append(left[i])#append用于在列表末尾添加新内容
            i +=1
        else:
            res.append(right[j])
            j += 1
    res += left[i:]
    res += rught[j:]
    return res

第二种写法

def merge_Sort(alist):
    if len(alist) <= 1:
        return alist
    # 分割
    mid = len(alist)//2
    left = merge_Sort(alist[:mid])
    right = merge_Sort(alist[mid:])
    # 开始合并
    merge = []
    while left and right:
        if left[0] <= right[0]:
            merge.append(left.pop(0))
        else:
            merge.append(right.pop(0))
    merge.extend(right if right else left)
    return merge


L1 = [54, 34, 67, 32, 98, 7, 58, 43, 42, 41, 45, 65, 64, 53, 90]
print(merge_Sort(L1))

6、堆排序

##堆排序
def heap_sort(lists):
    size = len(lists)
    build_heap(lists, size)
    for i in range(0, size)[::-1]:
        lists[0], list[i] = list[i], lists[0]
        adjust_heap(lists, 0, i)
    return lists

#堆调整
def adjust_heap(lists, i, size):
    lchild = 2 * i + 1
    rchild = 2 * i + 2
    maxi = i
    if lchild < size and lists[maxi] < lists[lchild]:
        maxi = lchild
    if rchild < size and lists[maxi] < lists[rchild]:
        maxi = rchild
    if maxi !=i:
    #在做了堆调整之后,做对调值操作
        lists[maxi],lists[i] = lists[i],lists[maxi]
        adjust_heap(lists, maxi, size)

#堆构建
def build_heap(lists,size):
    for i in range(0, int(size/2))[::-1]:
        adjust_heap(lists, i, size)

7、快速排序

基本思想:分而治之

def Quick_Sort(alist):
    quicksortHelper(alist, 0, len(alist)-1)


def quicksortHelper(alist, low, high):
    if low < high:
        q = Partition(alist, low, high)
        quicksortHelper(alist, low, q-1)
        quicksortHelper(alist, q+1, high)


def Partition(alist, low, high):
    pivot = alist[low]
    left = low
    right = high

    while left < right:
        while left < right and alist[right] >= pivot:
            right = right - 1
        if left < right:
            alist[left] = alist[right]
            left = left + 1
        while left < right and alist[left] <= pivot:
            left = left +1
        if right <= left:
            alist[right] = alist[left]
            right = right - 1
    alist[left] = pivot
    return left

alist = [1, 3, 2, 4]
Quick_Sort(alist)
print(alist)

8、计数排序

基本思想:对于某个元素a,检查有几位元素小于a记为i,然后把a放到序号为i的位置

def count_sort(a,k):#a为数组,k为max(a)
    n = len(a)
    b = [0 for i in range(n)] #设置输出序列,并初始化为0
    c = [0 for i in range(k+1)] #设置计数序列并初始化为0
    for j in a:
        c[j] = c[j] + 1
    for i in range(1, len(c)):
        c[i] = c[i] + c[i-1]
    for j in a:
        b[c[j] - 1] = j
        c[j] = c[j] - 1
    return b

9、桶排序

基本思想:把数组分为若干个等长区间,每个子数组各自排序,最后合并

def bucket_sort(a):
    buckets = [0] * ((max(a)-min(a))+1) #初始化桶元素为0
    for i in range(len(a)):
        buckets[a[i] - min(a)] +=1 #遍历数组a,在桶的相应位置累加值
        b = []
        for i in range(len(buckets)):
            if bucket[i] != 0:
                b += [i + min(a)] * buckets[i]
        return b

10、基数排序

基本思想:将待排序的数据按照位数切割成不同的数字,然后按每个位数分别比较

def radix_sort(list, d = 3): #默认三位数,可以自定义修改
    for i in range(d): #d轮排序
        s = [[] for k in range(10)] #因为每一位数字都是0-9,所以建10个桶
        for j in list:
            s[int(j / (10 ** i)) % 10].append(j)
        re = [a for b in s for a in b]
    return re
  • 16
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值