当前位置:网站首页>第 1 章 一大波数正在靠近——排序
第 1 章 一大波数正在靠近——排序
2022-08-09 21:54:00 【阿瑟与非】
第 1 节 最快最简单的排序——桶排序
# 第 1 节 最快最简单的排序——桶排序
import numpy as np
# 时间复杂度 O(M+N)
# M:桶的个数
# N:待排序数的个数
def sort(a):
b = np.zeros(11, dtype=int)
# 进行计数
for v in a:
temp = b[v]
b[v] = temp + 1
print(b)
print('排序结果:', end="\t")
# 出现了几次就打印几次
for (i, v) in enumerate(b):
for j in range(1, v + 1):
print(i, end="\t")
if __name__ == '__main__':
array = [5, 2, 5, 3, 8]
sort(array)第 2 节 邻居好说话——冒泡排序
冒泡排序的基本思想是:每次比较两个相邻的元素,如果它们的顺序错误就把它们交换过来。
# 第 2 节 邻居好说话——冒泡排序
def bubble_sort(a):
print(a)
n = len(a)
for i in range(n): # 比较n-1趟
for j in range(n - i - 1):
# 从第一个数开始和后面的数比较,比较次数:n-i-1
if a[j] < a[j + 1]:
t = a[j + 1]
a[j + 1] = a[j]
a[j] = t
if __name__ == '__main__':
a = [23, 456, 78, 8, 9, 23, 561, 323, 11, 8]
bubble_sort(a)
print("结果:")
print(a)边栏推荐
- STC8H Development (15): GPIO Drives Ci24R1 Wireless Module
- 论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
- 2.1.5 大纲显示问题
- AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero
- In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture
- 国内手机厂商曾为它大打出手,如今它却最先垮台……
- SQLi-LABS Page-2 (Adv Injections)
- 6 rules to sanitize your code
- 聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
- ACM MM 2022 | Cloud2Sketch: Painting with clouds in the sky, AI brush strokes
猜你喜欢

每日一R「02」所有权与 Move 语义

Blender程序化建模简明教程【PCG】

How do task flow executors work?

TF generates uniformly distributed tensor

xctf攻防世界 Web高手进阶区 ics-05

JSON 基本使用
![[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]](/img/49/ebedcd4d27aa608360ac17e504f36d.png)
[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]

2022年中国第三方证券APP创新专题分析

宝塔实测-搭建LightPicture开源图床系统

Converting angles to radians
随机推荐
Pagoda measurement - building LightPicture open source map bed system
一本通2074:【21CSPJ普及组】分糖果(candy)
shell学习
【微服务~Nacos】Nacos之配置中心
Space not freed after TRUNCATE table
SQLi-LABS Page-2 (Adv Injections)
This article lets you quickly understand implicit type conversion [integral promotion]!
Flask introductory learning tutorial
Ehrlich screening method: Counting the number of prime numbers
Shanghai Konan SmartRocket series product introduction (3): SmartRocket iVerifier computer interlocking system verification tool
小程序+自定义插件的关键性
Install win virtual machine on VMware
In programming languages, the difference between remainder and modulo
台风生成,广州公交站场积极开展台风防御安全隐患排查
OKR 锦囊妙计
Basic JSON usage
电脑系统重装后怎么用打印机扫描出文件?
十步以内,用小程序快速生成App!
Usage of placeholder function in Tensorflow
【GORM】模型关系-HasMany关系