当前位置:网站首页>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=' ')
边栏推荐
- MYSQL logical architecture
- 【历史上的今天】7 月 31 日:“缸中之脑”的提出者诞生;Wi-Fi 之父出生;USB 3.1 标准发布
- cmake入门学习笔记
- The principle of virtual inheritance
- How is the tree structure of the device tree reflected?
- What is the meaning of JS timestamp?Know SQL will consider to add a timestamp, JS timestamp for the scene?
- WebApi hits an Attribute to handle exceptions uniformly
- 500 miles
- WeChat applet page syntax
- STK8321 I2C (Shengjia-accelerometer) example
猜你喜欢
随机推荐
ECCV2022 Workshop | Multi-Object Tracking and Segmentation in Complex Environments
如何编辑epub电子书的目录
sqlserver cannot connect remotely
Fat interface in JQESAP system
Google "Cloud Developer Quick Checklist"; Tsinghua 3D Human Body Dataset; SenseTime "Universal Vision Framework" open class; Web3 Minimalist Getting Started Guide; Free Books for Efficient Deep Learni
RTL8762DK uses DebugAnalyzer (four)
MYSQL事务
修改Postman安装路径
High dimensional Gaussian distribution basics
YOLO怎么入门?怎么实现自己的训练集?
IDEA调试
Game Security 03: A Simple Explanation of Buffer Overflow Attacks
C string array reverse
What is the meaning of JS timestamp?Know SQL will consider to add a timestamp, JS timestamp for the scene?
/usr/sbin/vmware-authdlauncher: error while loading shared libraries: libssl.so.1.0.2*Solution
Rasa 3.x 学习系列-使用查找表改进实体提取
解决IDEA默认情况下新建文件时,右击,new,没有XML文件的问题
Summary of JVM interview questions (continuously updated)
OSF一分钟了解敏捷开发模式
MYSQL查询截取优化分析









