当前位置:网站首页>快速排序(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]
边栏推荐
- Liberg avenue to Jane series
- Oc/swift Technology Download File (breakpoint continuation AFN download file alamofire Download File native download) (source code)
- RNN循环神经网络
- 【BM2 链表内指定区间反转】
- Flex layout
- 【Day_02 0419】倒置字符串
- [pytorch] picture enlargement
- [day_070425] Fibonacci series
- Design principle of infrared circuit of single chip microcomputer
- CONDA virtual environment envs directory is empty
猜你喜欢

【pytorch】CNN实战-花朵种类识别

What are the aspects of performance testing? What are the classification and testing methods?

Oc/swift Technology Download File (breakpoint continuation AFN download file alamofire Download File native download) (source code)
![[Web3 series development tutorial - create your first NFT (4)] what can NFTs bring to you](/img/57/f263f3f3c40b1440b0cbb58c5e05a5.jpg)
[Web3 series development tutorial - create your first NFT (4)] what can NFTs bring to you
![[C language] file operation](/img/19/5bfcbc0dc63d68f10155e16d99581c.png)
[C language] file operation

【Day_06 0423】不要二
![[1] Basic knowledge of mathematical modeling](/img/29/90b1c7533e9443852758d10080e239.png)
[1] Basic knowledge of mathematical modeling

力扣5: 最长回文子串

Multi target detection

C language file operation
随机推荐
英语句式参考纯享版 - 状语从句
Regular expressions and calling related functions in C language
[nanny level] package volume optimization tutorial
【pytorch】CNN实战-花朵种类识别
【无标题】
Decomposing a positive integer into prime factors requires decomposing into as many factors as possible.
[untitled]
【Day_03 0420】字符串中找出连续最长的数字串
【Day_06 0423】把字符串转换成整数
[C language] file operation
机械制造企业如何借助ERP系统,做好生产管理?
What is KVM? What is KVM virtual machine?
Nuxt configuration topic switching
Servlet cannot directly obtain JSON format data in request request
[Web3 series development tutorial - create your first NFT (4)] what can NFTs bring to you
C language file operation
[pytorch] fine tuning technology
抖音web端 s_v_web_id 参数生成分析与实现
Code runner for vs code, with more than 40million downloads! Support more than 50 languages
[day04_0421] C language multiple choice questions