当前位置:网站首页>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=' ')
边栏推荐
- RTL8762DK uses DebugAnalyzer (four)
- IDEA debugging
- 从零造键盘的键盘超级喜欢,IT人最爱
- MYSQL-批量插入数据
- What practical projects can machine learning beginners learn?
- 树莓派 的 arm 版的 gcc 安装 和环境变量的配置
- Beijing suddenly announced that yuan universe big news
- IDEA modifies the annotation font
- leetcode: 1648. Color ball with decreasing sales value [Boundary find by two points]
- RTL8762DK PWM (seven)
猜你喜欢
IDEA debugging
你需要知道的 TCP 四次挥手
YOLO怎么入门?怎么实现自己的训练集?
Google engineer fired for claiming AI awareness: breach of nondisclosure agreement
leetcode:1562. 查找大小为 M 的最新分组【模拟 + 端点记录 + 范围合并】
Summary of MVCC
机器学习初学者可以学哪些实战项目?
Unity3D study notes 10 - texture array
【 】 today in history: on July 31, "brains in vats" the birth of the participant;The father of wi-fi was born;USB 3.1 standard
Flink deploys and submits jobs
随机推荐
date command
Simple vim configuration
/usr/sbin/vmware-authdlauncher: error while loading shared libraries: libssl.so.1.0.2*解决办法
软考高级系统架构设计师系列之:系统开发基础知识
The IDEA can't find or unable to load The main class or Module "*" must not contain The source root "*" The root already belongs to The Module "*"
Google Earth Engine - Error resolution of Error: Image.clipToBoundsAndScale, argument 'input': Invalid type
Academicians of the two academies speak bluntly: Don't be superstitious about academicians
Google engineer fired for claiming AI awareness: breach of nondisclosure agreement
两院院士直言:不要迷信院士
MYSQL Keyword Explain Analysis
RTL8762DK PWM (seven)
网关gateway跨域
Flink deploys and submits jobs
500 miles
leetcode: 1562. Find latest grouping of size M [simulation + endpoint record + range merge]
Introduction to the decision logic of WAASAP WebClient UI page labels
By Value or By Reference
【数据分析】基于matlab GUI学生成绩管理系统【含Matlab源码 1981期】
Device tree - conversion from dtb format to struct device node structure
Basic implementation of vector