当前位置:网站首页>Understanding of bubble sort
Understanding of bubble sort
2022-08-07 15:13:00 【python_problem】
Table of Contents
Code for bubble sort
def bubbling(list):for i in range(len(list)-1):for l in range(len(list)-1-i):if list[l] > list[l+1]:list[l], list[l + 1] = list[l + 1], list[l]return listif __name__ == '__main__':list = [3,1,44,5,2,30,0]print(bubbling(list))The process of bubble sorting
Comparing two adjacent numbers, the larger number is placed at the back, and the largest number will be sorted last after the row is complete

In the second round of comparison, only the remaining numbers are compared, and the largest number is still ranked last, and the number of times used is reduced by one

Three rounds of sorting are required for the four numbers

Write Code
First write the first round of sorting, 4 numbers need to be sorted 3 times, if n numbers need to be sorted n-1 times
list = [4,3,2,1]for l in range(len(list)-1): # len(list) is the length of the list 4, which needs to be sorted (4-1) timesif list[l] > list[l+1]: # Compare two adjacent numbers, if the former is greater than the latterlist[l], list[l + 1] = list[l + 1], list[l] # then swap the two positionsAfter the first round of sorting is completed, continue to perform multiple rounds of sorting, enter a multi-round sorting loop, nest multiple rounds of sorting loops on the outside, 4 numbers need 3 rounds, if n numbers need to be sorted n-1 round
And the number of rounds and the number of rounds have the following relationship

So get the code
list = [4,3,2,1]n = len(list) # len(list) is the length of the list 4for i in range(n-1) # This loop is a round number loop, n numbers need to be sorted for n-1 roundsfor l in range(n-i-1): # This loop is the number of times of each round, and the number of times of each round is n-rounds-1if list[l] > list[l+1]: # Compare two adjacent numbers, if the former is greater than the latterlist[l], list[l + 1] = list[l + 1], list[l] # then the two are swapped边栏推荐
- win10 uwp 圆角按钮
- Hash table | The sum of three numbers, the sum of four numbers | The most suitable `double pointer method` | leecode brush notes
- LeetCode每日一题(911. Online Election)
- QT—状态机框架
- 【Verilog】时序逻辑电路 -- 有限同步状态机[补充]
- 【通信原理】第三章 -- 随机过程[下]
- 微信小程序——小程序去除文本中的<br>标签
- C Expert Programming Chapter 7 Thinking About Memory 7.8 Take it easy --- "Thing King" and "Page Game"
- 联盛德W801系列2-WIFI一键配网,信息保存
- Lianshengde W801 series 1-flash save data routine: save wifi distribution network information
猜你喜欢

06 【Generic】
![[Principle of Database System] Chapter 4 Advanced Database Model: E/R Model and Its Design Rules and Constraints](/img/67/c6ee5068487bb812c8092fce7ad81c.png)
[Principle of Database System] Chapter 4 Advanced Database Model: E/R Model and Its Design Rules and Constraints
![【通信原理】第三章 -- 随机过程[补充]](/img/47/0607ab885999d85dfcc3f8ef00c38a.png)
【通信原理】第三章 -- 随机过程[补充]

Hash table | The sum of three numbers, the sum of four numbers | The most suitable `double pointer method` | leecode brush notes

TCP/UDP协议

俩日总结(【20】【21】)

【数据库系统原理】第三章 BC范式、第三范式和第四范式

基于FPGA的VGA显示彩条、字符、图片

文件管理:文件存储空间管理

基于RK3566中RTL8201F网口百兆调试笔记
随机推荐
RPG game map scene management and maintenance (server)
微信自动发卡机器人说明
分库分表和分布式acp和分布式事务
触摸屏如何利用无线PPI通信模块远程采集PLC数据?
打印从1到最大的n位数------2022/08/05
服务器管理面板aaPanel使用中的一些问题汇总
测开面经汇总
Lianshengde W801 series 1-flash save data routine: save wifi distribution network information
LeetCode HOT HOT 100 (9. Telephone number letter combinations)
微信小程序——小程序去除文本中的<br>标签
pip使用豆瓣镜像源
TCP/UDP协议
RTT学习笔记10- 设备IPC 完成量-ringbufffer-workwueue
MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
【数据库系统原理】第四章 高级数据库模型:弱实体集、E/R 联系到关系的转化、子类结构到关系的转化
Programming Experts in C Chapter 8 Why Programmers Can't Tell the Difference Between Halloween and Christmas 8.1 The Portzebie Weights and Measures System
苹果,设计思维的“布道者”
项目进度管理
俩日总结(【18】、【19】)
65:第五章:开发admin管理服务:18:开发【根据条件,分页查询用户列表,接口】;(使用了Converter转换器,把url中String格式的日期,转成Date格式的;)