• 周四. 8月 11th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

python实现8大排序

admin

11月 28, 2021

1.冒泡排序

# 第一遍结束,最大的数在最后
def sort(alist):
    for i in range(len(alist)):
        for j in range(i,len(alist)):
            if alist[i] > alist[j]:
                alist[i],alist[j] = alist[j],alist[i]
    return alist
print(sort([3,1,4,2,5]))

2.快速排序

def sort(alist):
    if len(alist) < 2:
        return alist
    base_node = alist.pop(0)
    left_alist = []
    right_alist = []

    for i in alist:
        if i > base_node:
            right_alist.append(i)
        else:
            left_alist.append(i)
    return sort(left_alist) + [base_node] + sort(right_alist)
print(sort([3,10,7,6,9]))

3.简单选择排序

# 比较+交换,每次挑选最小的放在前面
def sort(alist):
    for i in range(len(alist)):
        min_alist_index = i 
        for j in range(i,len(alist)):
            if alist[min_alist_index] > alist[j]:
                min_alist_index = j
        alist[min_alist_index],alist[i] = alist[i],alist[min_alist_index]
    return alist
print(sort([9,1,7,2,5,3]))

4.插入排序

# 向前遍历
def sort(alist):
    for i in range(len(alist)):
        for j in range(0,i):
            if alist[j] > alist[i]:
                alist[j],alist[i] = alist[i],alist[j]
    return alist
print(sort([3,1,2,6,5,7,4]))

5.希尔排序

# 将待排序数组按照步长gap进行分组,
# 然后将每组的元素利用直接插入排序的方法进行排序,每次将gap折半减少,循环以上操作。
# 当gap=1的时候,利用直接插入完成排序
def sort(alist):
    length = len(alist)
    gap = length // 2
    while gap >= 1:
        for j in range(gap,length):
            i = j
            while(i - gap) >= 0:
                if alist[i] < alist[i - gap]:
                    alist[i],alist[i-gap] = alist[i - gap],alist[i]
                    i -= gap
                else:
                    break
        gap //= 2
    return alist
print(sort([4,6,2,1,5,3]))

6.归并排序

# 思想:将N个长度为1的键值,成对合并成N/2个长度为2的键值组
#       将N/2     2                 N/4        4
#         N/4     4                 N/8        8
#         ...
def sort(alist):
    if len(alist) <= 1:
        return alist
    middle = len(alist) // 2
    left_alist = sort(alist[:middle])
    right_alist = sort(alist[middle:])

    i = j = 0
    result_list = []
    while i < len(left_alist) and j < len(right_alist):
        if left_alist[i] <= right_alist[j]:
            result_list.append(left_alist[i])
            i += 1
        else:
            result_list.append(right_alist[i])
            j += 1
    result_list += left_alist[i:]
    result_list += right_alist[j:]

    return result_list
print(sort([4,2,6,1,5,3]))

7.桶排序

def tong_sort(alist):
    max_num = max(alist) # 选择一个最大的数
    bucket = [0]*(max_num+1) # 创建一个元素全为0的列表。当做桶。

    for i in alist:  # 把所有元素放入桶中,即把对应的元素个数加一
        bucket[i] += 1
        sort_nums = []  # 存储桶中的元素

    for j in range(len(bucket)):   # 取出桶中的元素
        if bucket[j] != 0:
            for y in range(bucket[j]):
                sort_nums.append(j)
    return sort_nums
print(tong_sort([5,3,1,4,2,6]))

8.堆排序

def heapify_sort(alist,n,i):
    largest = i
    left = 2 * i + 1  
    right = 2 * i + 2

    if left < n and alist[i] < alist[left]:
        largest = left

    if right < n and alist[largest] < alist[right]:
        largest = right

    if largest != i:
        alist[i],alist[largest] = alist[largest],alist[i] # 交换 

        heapify_sort(alist,n,largest)

def heap_sort(alist):
    n = len(alist)

    for i in range(n,-1,-1):
        heapify_sort(alist,n,i)

    # 一个个的交换元素
    for i in range(n-1,0,-1):
        alist[i],alist[0] = alist[0],alist[i] # 交换
        heapify_sort(alist,i,0)

alist = [12,11,13,5,4,6]
heap_sort(alist)
n = len(alist)
for i in range(n):
    print(alist[i])

发表评论

您的电子邮箱地址不会被公开。