当前位置:网站首页>排序问题:冒泡排序,选择排序,插入排序
排序问题:冒泡排序,选择排序,插入排序
2022-07-26 06:34:00 【fiveym】
什么是列表排序
排序:将一组“无序”的记录序列调整为“有序”的记录序列
列表排序:将无序列表变为有序列表
输入:列表
输出:有序列表
升序与降序
内置排序函数sort()
sort是在源列表排序,sortted是创建一个新的排序后的列表,源列表不变。
常见排序算法介绍

排序算法分析
冒泡排序(Bubble Sort)
列表每相邻的数,如果前面比后面大,则交换这两个数
一趟排序完成后,则无序区减少一个数,有序区增加一个数
冒泡排序一般要进行多趟
代码关键点:趟,无序区范围
def bubble_sort(li):
for i in range(len(li)-1): #第i趟
exchange = False
for j in range(len(li)-i-1):
if li[j] < li[j+1]:
li[j], li[j+1] = li[j+1], li[j]
exchange = True
print(li)
if not exchange:
return
li = [9,8,7,1,2,3,4,5,6]
bubble_sort(li)
选择排序(select_sort)
- 一趟排序记录最小的数,放到第一个位置
- 在一趟排序记录记录列表无序区最小的数,放到第二个位置
- 算法关键点:有序区和无序区,无序区最小数的位置
def select_sort(li):
for i in range(len(li)-1): #i是第几趟
min_loc = i
for j in range(i+1, len(li)):
if li[j] < li[min_loc]:
min_loc = j
li[i], li[min_loc] = li[min_loc], li[i]
print(li)
li = [3,4,2,1,5,6,8,7,9]
print(li)
select_sort(li)
插入排序(insert_sort)
- 初始时手里(有序区)只有一张牌
- 每次(从无序区)摸一张牌,插入到手里已有牌的正确位置
def insert_sort(arry): #排序之前的列表
for idx in range(1, len(arry)):
j = idx
while j > 0 and arry[j] < arry[j-1]:
(arry[j-1], arry[j]) =(arry[j], arry[j-1])
j -= 1
return arry
arry = [1,2,5,7,6,9,8,4,3]
print(insert_sort(arry))
边栏推荐
- [image denoising] image denoising based on bicube interpolation and sparse representation matlab source code
- [pytorch] picture enlargement
- Latex merges multiple rows and columns of a table at the same time
- [day_070425] legal bracket sequence judgment
- PG Vacuum 杂谈之 auto vacuum
- 09 eth smart contract
- 性能测试包括哪些方面?分类及测试方法有哪些?
- Intelligent fire protection application based on fire GIS system
- 【保姆级】包体积优化教程
- Force deduction 5: Longest palindrome substring
猜你喜欢

Interpretation of TPS motion (cvpr2022) video generation paper

将一个正整数分解质因数,要求分解成尽可能小的多个的因数。

Alibaba cloud OSS binding custom domain name

机械制造企业如何借助ERP系统,做好生产管理?

PG vacuum auto vacuum

Force deduction 5: Longest palindrome substring

Leetcode:741. picking cherries

Decomposing a positive integer into prime factors requires decomposing into as many factors as possible.

原生高性能抓包工具Proxyman,送给爱学习的你
![[1] Basic knowledge of mathematical modeling](/img/29/90b1c7533e9443852758d10080e239.png)
[1] Basic knowledge of mathematical modeling
随机推荐
[pytorch] fine tuning technology
【BM2 链表内指定区间反转】
Registration conditions for system integration project management engineer (intermediate level of soft exam) in the second half of 2022
[day06_0423] C language multiple choice questions
@Constructorproperties annotation understanding and its corresponding usage
【图像去噪】基于双立方插值和稀疏表示实现图像去噪matlab源码
力扣——4. 寻找两个正序数组的中位数
[day_020419] sort subsequence
Read five articles in the evening | Economic Daily: don't treat digital collections as wealth making products
【无标题】
[day_070425] legal bracket sequence judgment
Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)
Go 的切片与数组
Convert amount figures to uppercase
RNN recurrent neural network
Servlet无法直接获取request请求中的JSON格式数据
How can machinery manufacturing enterprises do well in production management with the help of ERP system?
【Day_04 0421】进制转换
Mobile web
Facing the rebound market, how do we operate? 2020-03-21