排序-堆排序-算法分析和python 内置函数实现

算法分析:

"""
    说明:堆排序过程
    1、构造一个堆(之下而上的构建方式)
    2、取出堆根节点(即当前堆得最大值、或者最小值,根据当前是大根堆还是小根堆来确定)
    3、取完全二叉树最后的叶子节点放到根节点,进行向下调整
    4、重复3-4过程,直达堆完全为空
"""

代码实现:

# -*- coding: utf-8 -*
# 开发人员:charles
# 开发时间:2022/5/21 16:02
# 文件名称:heap_sort_by_bulit_in_function.py
"""
    说明:堆排序过程
    1、构造一个堆(农村包围城市)
    2、取出堆根节点(即当前堆得最大值、或者最小值,根据当前是大根堆还是小根堆来确定)
    3、取完全二叉树最后的叶子节点放到根节点,进行向下调整
    4、重复3-4过程,直到堆完全为空
参考链接:https://docs.python.org/3/library/heapq.html?highlight=heapq#heapq.heapify
"""
import heapq
import random

li = list(range(2, 20))
random.shuffle(li)
print("before sort\t\t", li)

# Transform list x into a heap, in-place, in linear time.
heapq.heapify(li)  # python 内置堆函数建立的是小根堆,想要大根堆的话,把所有的li通过map取反即可
print("construct\t\t", li)

# Push the value item onto the heap, maintaining the heap invariant.
heapq.heappush(li, 1)
print("heappush :\t\t", li)

# Push item on the heap, then pop and return the smallest item from the heap.
# The combined action runs more efficiently than heappush() followed by a separate call to heappop().
heapq.heappushpop(li, 21)
print("heappushpop:\t", li)

# Pop and return the smallest item from the heap, maintaining the heap invariant. If the heap is empty,
# IndexError is raised. To access the smallest item without popping it, use heap[0].
heapq.heappop(li)
print("heappop:\t\t", li)

# Pop and return the smallest item from the heap, and also push the new item.
# The heap size doesn’t change. If the heap is empty, IndexError is raised.
# # This one step operation is more efficient than a heappop() followed by heappush() and can be more appropriate when using a fixed-size heap.
# The pop/push combination always returns an element from the heap and replaces it with item.
# # The value returned may be larger than the item added. If that isn’t desired, consider using heappushpop() instead.
# Its push/pop combination returns the smaller of the two values, leaving the larger value on the heap.
heapq.heapreplace(li, 22)
print("heapreplace:\t", li)

# Return a list with the n largest elements from the dataset defined by iterable.
# key, if provided, specifies a function of one argument that is used to extract a comparison key from each element in iterable (for example, key=str.lower).
# Equivalent to: sorted(iterable, key=key, reverse=True)[:n].
print("return largest 5:", heapq.nlargest(5, li))

# Return a list with the n smallest elements from the dataset defined by iterable.
# key, if provided, specifies a function of one argument that is used to extract a comparison key from each element in iterable (for example, key=str.lower).
# Equivalent to: sorted(iterable, key=key)[:n].

newlist = heapq.nsmallest(5, li)
print("return smallest 1:", newlist)
newlist2 = heapq.nsmallest(5, li, key=lambda x: (x % 2 == 1, x))  # 只要偶数
print("return smallest 2:", newlist2)
newlist3 = heapq.nsmallest(5, li, key=lambda x: x + 1)
print("return smallest 3:", newlist3)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值