当前位置:网站首页>【经典排序】快速排序
【经典排序】快速排序
2022-08-10 23:50:00 【冰冷的希望】
1.快速排序
选取第一个数为基准值,把比基准值大的放在左边,小的放在右边,依次遍历,最后有序
快速排序总是优于归并排序
2.代码实现
def quick_sort(my_list, start, end):
if start >= end:
return
mid = my_list[start]
low = start
height = end
while low < height:
while low < height and mid <= my_list[height]:
height -= 1
my_list[low] = my_list[height]
while low < height and mid > my_list[low]:
low += 1
my_list[height] = my_list[low]
my_list[low] = mid
quick_sort(my_list, start, low - 1)
quick_sort(my_list, low + 1, end)
if __name__ == '__main__':
pass
a = [5, 6, 1, 9, 0, 4, 6, 8]
print(a)
quick_sort(a, 0, len(a) - 1)
print(a)
3.复杂度
最优时间复杂度: O(nlogn)
最坏时间复杂度: O(n^2)
平均时间复杂度: O(nlogn)
稳定性:不稳定
边栏推荐
- Ali P7 bask in January payroll: hard to fill the, really sweet...
- Multilingual Translation - Multilingual Translation Software Free
- [Excel knowledge and skills] Convert text numbers to numeric format
- IEEE的论文哪里可以下载?
- 5. Lombok
- [C language articles] Expression evaluation (implicit type conversion, arithmetic conversion)
- Three-column layout implementation
- Part of the reserve bank is out of date
- Introduction to Qt (6) - Implementation of the lottery system
- promise详解
猜你喜欢
学习Apache ShardingSphere解析器源码(一)
图片懒加载(纯手写)
开启新征程——枫叶先生第一篇博客
5. Lombok
ROS Experimental Notes - Install QPEP and Intel-MKL
[Excel knowledge and skills] Convert "false" date to "true" date format
YOLOv5的Tricks | 【Trick12】YOLOv5使用的数据增强方法汇总
PMP每日一练 | 考试不迷路-8.10(包含敏捷+多选)
There is no recycle bin for deleted files on the computer desktop, what should I do if the deleted files on the desktop cannot be found in the recycle bin?
Introduction to Qt (6) - Implementation of the lottery system
随机推荐
16. File upload
sqlmap combined with dnslog fast injection
Software Testing Certificate (1) - Software Evaluator
5. Lombok
Software protection scenario of NOR FLASH flash memory chip ID application
SQL injection base - order by injection, limit, wide byte
英文文献阅读时,如何做笔记?
15. Interceptor - HandlerInterceptor
工程师如何对待开源
YOLOv5的Tricks | 【Trick11】在线模型训练可视化工具wandb(Weights & Biases)
Dump file generation, content, and analysis
ROS实验笔记之——UZH-FPV数据集的验证
【C语言篇】操作符之 位运算符详解(“ << ”,“ >> ”,“ & ”,“ | ”,“ ^ ”,“ ~ ”)
推进牛仔服装的高质量发展
How to quickly grasp industry opportunities and introduce new ones more efficiently is an important proposition
C language% (%d,%c...)
LENS CRA和SENSOR CRA匹配问题解析
11. 自定义转换器
In 22 years, the salary of programmers nationwide in January was released, only to know that there are so many with annual salary of more than 400,000?
Mysql.慢Sql