当前位置:网站首页>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 usage concepts of vim
- Key Points Estimation and Point Instance
- RTL8762DK RTC(五)
- Luogu P3373: 线段树
- OSF understands the agile development model in one minute
- MYSQL查询截取优化分析
- IDEA 找不到或无法加载主类 或 Module “*“ must not contain source root “*“ The root already belongs to module “*“
- [Data analysis] Based on matlab GUI student achievement management system [including Matlab source code 1981]
- 软考高级系统架构设计师系列之:信息系统基础知识
- 你需要知道的 TCP 四次挥手
猜你喜欢
RTL8762DK Lighting/LED (3)
How is the tree structure of the device tree reflected?
【 】 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
【Cryptography/Cryptanalysis】Cryptanalysis method based on TMTO
YOLO怎么入门?怎么实现自己的训练集?
leetcode:1562. 查找大小为 M 的最新分组【模拟 + 端点记录 + 范围合并】
【数据分析】基于matlab GUI学生成绩管理系统【含Matlab源码 1981期】
IDEA modifies the annotation font
Academicians of the two academies speak bluntly: Don't be superstitious about academicians
数据中台建设(七):数据资产管理
随机推荐
声称AI存在意识,谷歌工程师遭解雇:违反保密协议
Inheritance Considerations
Summary of MVCC
C字符串数组反转
解决安装MySQL后,Excel打开很慢的问题
MYSQL Classic Interview Questions
leetcode: 1648. Color ball with decreasing sales value [Boundary find by two points]
Completely closed Chrome updated and in the top right corner of the tip
Game Security 03: A Simple Explanation of Buffer Overflow Attacks
RTL8762DK PWM (seven)
IDEA debugging
RTL8762DK WDG(六)
GDB source code analysis series of articles five: dynamic library delay breakpoint implementation mechanism
RTL8762DK Lighting/LED (3)
Detailed explanation of TCP protocol
OSF understands the agile development model in one minute
The kernel of the decompression process steps
MYSQL master-slave replication
RTL8762DK 点灯/LED(三)
手写二叉查找树及测试