当前位置:网站首页>快速排序(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]
边栏推荐
- C language introduction practice (7): switch case calculation of days in the year (normal year / leap year calculation)
- Swift basic FileManager (file management)
- JS date details, string to date
- 力扣5: 最长回文子串
- [day06_0423] C language multiple choice questions
- TPS Motion(CVPR2022)视频生成论文解读
- Redis sentinel cluster setup
- 【Day_07 0425】合法括号序列判断
- 【Day05_0422】C语言选择题
- 深度学习——CV、CNN、RNN
猜你喜欢
![[1] Basic knowledge of mathematical modeling](/img/29/90b1c7533e9443852758d10080e239.png)
[1] Basic knowledge of mathematical modeling

Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)

Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system

YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors

IP day 10 notes - BGP

RNN循环神经网络
![[1]数学建模基础入门知识](/img/29/90b1c7533e9443852758d10080e239.png)
[1]数学建模基础入门知识

Go 的切片与数组

带你搞透IO多路复用原理(select、poll和epoll)

Intelligent fire protection application based on fire GIS system
随机推荐
Input the records of 5 students (each record includes student number and grade), form a record array, and then output them in order of grade from high to low The sorting method adopts selective sortin
Why the server is stuck
[Web3 series development tutorial - create your first NFT (4)] what can NFTs bring to you
[day_070425] legal bracket sequence judgment
Liberg avenue to Jane series
[day_040421] calculate candy
If I want to listen to Jay Chou with you, I want you to listen to my whole youth
机械制造企业如何借助ERP系统,做好生产管理?
抖音web端 s_v_web_id 参数生成分析与实现
The number of weeks of Oracle last year and this year, with the start time and end time
PG vacuum auto vacuum
【Day05_0422】C语言选择题
[1]数学建模基础入门知识
【pytorch】微调技术
YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
C语言进阶——可存档通讯录(文件)
[day06_0423] C language multiple choice questions
Convert amount figures to uppercase
性能测试包括哪些方面?分类及测试方法有哪些?
Three skills are needed to engage in SAP related work