当前位置:网站首页>785. 快速排序
785. 快速排序
2022-08-01 01:21:00 【aJupyter】
Question
给定你一个长度为 n 的整数数列。
请你使用快速排序对这个数列按照从小到大进行排序。
并将排好序的数列按顺序输出。
输入格式
输入共两行,第一行包含整数 n。
第二行包含 n 个整数(所有整数均在 1∼109 范围内),表示整个数列。
输出格式
输出共一行,包含 n 个整数,表示排好序的数列。
数据范围
1≤n≤100000
输入样例:
5
3 1 2 4 5
输出样例:
1 2 3 4 5
Ideas
快排
Code
''' 快排流程 - 1、确定分界点 - 2、确定区间(朴素方法:开数组;优化方法双指针) - 3、递归排序 '''
def quick_sort(q,l,r):
if l >= r:return
x = q[l+r>>1]
i,j = l-1,r+1
while i < j:
while 1:
i += 1
if q[i] >= x:
break
while 1:
j -= 1
if q[j] <= x:
break
if i < j:
q[i],q[j] = q[j],q[i]
quick_sort(q,l,j)
quick_sort(q,j+1,r)
n = int(input())
lis = list(map(int,input().strip().split()))
quick_sort(lis,0,n-1)
for i in lis:
print(i,end=' ')
边栏推荐
- Basic implementation of vector
- MYSQL Index Analysis
- sqlserver无法远程连接
- GDB source code analysis series of articles five: dynamic library delay breakpoint implementation mechanism
- MYSQL-Batch insert data
- Data Middle Office Construction (VII): Data Asset Management
- Rasa 3.x Study Series - Rasa - Issues 4898 Study Notes
- YOLO怎么入门?怎么实现自己的训练集?
- Inheritance Considerations
- VPGNet
猜你喜欢

how to edit the table of contents of an epub ebook

MYSQL逻辑架构
![leetcode: 1562. Find latest grouping of size M [simulation + endpoint record + range merge]](/img/72/f52b5d3dcdb271f096c3e5108f0042.png)
leetcode: 1562. Find latest grouping of size M [simulation + endpoint record + range merge]

如何编辑epub电子书的目录

声称AI存在意识,谷歌工程师遭解雇:违反保密协议
![leetcode: 1648. Color ball with decreasing sales value [Boundary find by two points]](/img/b9/7bd33bd981ace25e3bbfc7be9117ff.png)
leetcode: 1648. Color ball with decreasing sales value [Boundary find by two points]

Introduction to machine learning how to?

解决安装MySQL后,Excel打开很慢的问题

IDEA修改注释字体

How is the tree structure of the device tree reflected?
随机推荐
/usr/sbin/vmware-authdlauncher: error while loading shared libraries: libssl.so.1.0.2*解决办法
RTL8762DK PWM (seven)
机器学习初学者可以学哪些实战项目?
mySql data view
【元胞自动机】基于matlab界面聚合元胞自动机模拟【含Matlab源码 2004期】
ECCV2022 Workshop | 复杂环境中的多目标跟踪和分割
How is the tree structure of the device tree reflected?
Summary of JVM interview questions (continuously updated)
Daily practice of LeetCode - Circular linked list question (interview four consecutive questions)
Basic implementation of vector
GDB source code analysis series of articles five: dynamic library delay breakpoint implementation mechanism
IDEA修改注释字体
解决安装MySQL后,Excel打开很慢的问题
解决IDEA默认情况下新建文件时,右击,new,没有XML文件的问题
Item 36: Specify std::launch::async if asynchronicity is essential.
Introduction to the decision logic of WAASAP WebClient UI page labels
IDEA无法识别module(module右下角没有蓝色小方块)
设备树——dtb格式到struct device node结构体的转换
Inheritance and friend, static member relationship
What is the meaning of JS timestamp?Know SQL will consider to add a timestamp, JS timestamp for the scene?