当前位置:网站首页>快速排序(quick-sort)
快速排序(quick-sort)
2022-07-26 06:34:00 【fiveym】
快速排序思路:
- 去一个元素p(列表中第一个元素),是元素归位
- 列表被分为两部分,左边都比p小,右边都比p大
- 递归完成排序

partition函数:归位函数,算出中间值的下标位置
每一个partition函数的时间复杂度为logn,一共有n次
所以快速排序的时间复杂度为O(nlogn)(最好情况下)
def partition(li, left, right):
tmp = li[left]
while left < right:
while left < right and li[right] >= tmp: #从右边找tmp小的数
right -= 1 #往左走一步
li[left] = li[right] #把右边的值写道左边的空位上
print(li, 'right')
while left < right and li[left] <= tmp:
left += 1
li[right] = li[left] #把左边的值写道右边空位上
print(li, 'left')
li[left] = tmp #两值相等之后把tmp归位
return left #返回mid的值
def quick_sort(li, left, right):
if left < right: #至少两个元素
mid = partition(li, left, right)
quick_sort(li, left, mid-1)
quick_sort(li, mid+1, right)
li=[5,7,4,6,3,1,2,9]
quick_sort(li, 0, len(li)-1)
print(li)
#结果
[2, 7, 4, 6, 3, 1, 2, 9, 8] right
[2, 7, 4, 6, 3, 1, 7, 9, 8] left
[2, 1, 4, 6, 3, 1, 7, 9, 8] right
[2, 1, 4, 6, 3, 6, 7, 9, 8] left
[2, 1, 4, 3, 3, 6, 7, 9, 8] right
[2, 1, 4, 3, 3, 6, 7, 9, 8] left
[1, 1, 4, 3, 5, 6, 7, 9, 8] right
[1, 1, 4, 3, 5, 6, 7, 9, 8] left
[1, 2, 3, 3, 5, 6, 7, 9, 8] right
[1, 2, 3, 3, 5, 6, 7, 9, 8] left
[1, 2, 3, 4, 5, 6, 7, 9, 8] right
[1, 2, 3, 4, 5, 6, 7, 9, 8] left
[1, 2, 3, 4, 5, 6, 7, 9, 8] right
[1, 2, 3, 4, 5, 6, 7, 9, 8] left
[1, 2, 3, 4, 5, 6, 7, 8, 8] right
[1, 2, 3, 4, 5, 6, 7, 8, 8] left
[1, 2, 3, 4, 5, 6, 7, 8, 9]
边栏推荐
- [day_060423] no two
- 【Day_01 0418】删除公共字符串
- 【Day_07 0425】合法括号序列判断
- Go 的切片与数组
- Huawei cloud koomessage is a new marketing weapon in the hot public beta
- Code runner for vs code, with more than 40million downloads! Support more than 50 languages
- Swift basic FileManager (file management)
- Why the server is stuck
- 将一个正整数分解质因数,要求分解成尽可能小的多个的因数。
- Leetcode:741. picking cherries
猜你喜欢

【保姆级】包体积优化教程

Multi target detection

Find the original root

Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言
![[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code](/img/2a/b5214e9fa206f1872293c9b9d7bdb6.png)
[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code

YOLOv6:又快又准的目标检测框架开源啦
![[day04_0421] C language multiple choice questions](/img/18/42924b5994b385a3cf132742141d88.png)
[day04_0421] C language multiple choice questions

Overview of image classification of vision transformer must read series
![[day05_0422] C language multiple choice questions](/img/cb/0019ec819480bd9f52055e726b323a.png)
[day05_0422] C language multiple choice questions

If introduced according to the open source framework
随机推荐
[pytorch] picture enlargement
Go channel
[1]数学建模基础入门知识
[nanny level] package volume optimization tutorial
【pytorch】微调技术
【C语言】文件操作
【Day_06 0423】把字符串转换成整数
[untitled]
输入5个学生的记录(每条记录包括学号和成绩), 组成记录数组, 然后按照成绩由高到低的次序输出. 排序方法采用选择排序
Latex merges multiple rows and columns of a table at the same time
[day_030420] find the longest consecutive number string in the string
Map dictionary and constraints of go
If I want to listen to Jay Chou with you, I want you to listen to my whole youth
Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)
Leetcode:934. The shortest Bridge
【Day_06 0423】不要二
@Constructorproperties annotation understanding and its corresponding usage
[Web3 series development tutorial - create your first NFT (4)] what can NFTs bring to you
【Day_04 0421】计算糖果
[1] Basic knowledge of mathematical modeling