当前位置:网站首页>785. Quick Sort
785. Quick Sort
2022-08-01 01:36: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、确定区间(朴素方法:开数组;Optimization method, double pointer) - 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=' ')
边栏推荐
猜你喜欢
Beijing suddenly announced that yuan universe big news
IDEA 找不到或无法加载主类 或 Module “*“ must not contain source root “*“ The root already belongs to module “*“
Unity3D study notes 10 - texture array
SC7A20 (Silan Micro-Accelerometer) Example
机器学习应该如何入门?
Summary of JVM interview questions (continuously updated)
ARM 交叉编译
带wiringPi库在unbutu 编译 并且在树莓派运行
元宇宙改变人类工作模式的四种方式
更换树莓派内核
随机推荐
Js replication
OSF understands the agile development model in one minute
What practical projects can machine learning beginners learn?
OSF一分钟了解敏捷开发模式
500 miles
The device node structure is converted into a platform_device structure
RTL8762DK PWM (seven)
Summary of JVM interview questions (continuously updated)
gateway gateway cross domain
YOLO怎么入门?怎么实现自己的训练集?
You need to know the TCP wave four times
Blueprint: Yang Hui's Triangular Arrangement
ECCV2022 Workshop | 复杂环境中的多目标跟踪和分割
【分层强化学习】HIRO:Data-Efficient Hierarchical Reinforcement Learning
sqlserver cannot connect remotely
软考高级系统架构设计师系列之:信息系统基础知识
链式编程、包、访问权限
Nmap 操作手册 - 完整版
ECCV2022 Workshop | Multi-Object Tracking and Segmentation in Complex Environments
GDB 源码分析系列文章五:动态库延迟断点实现机制