当前位置:网站首页>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=' ')
边栏推荐
- By Value or By Reference
- IDEA debugging
- Blueprint: Yang Hui's Triangular Arrangement
- Rasa 3.x 学习系列- Rasa - Issues 4918 学习笔记
- LeetCode每日一练 —— 环形链表问题(面试四连问)
- Four ways the Metaverse is changing the way humans work
- js 实现复制功能
- OSD读取SAP CRM One Order应用日志的优化方式
- RTL8762DK RTC (5)
- /usr/sbin/vmware-authdlauncher: error while loading shared libraries: libssl.so.1.0.2*Solution
猜你喜欢

MYSQL索引解析

MYSQL Index Analysis

Modern Enterprise Architecture Framework 1

RTL8762DK UART (two)

MYSQL logical architecture
![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]

RTL8762DK PWM (seven)

两院院士直言:不要迷信院士

MYSQL two-phase commit

Daily practice of LeetCode - Circular linked list question (interview four consecutive questions)
随机推荐
手写二叉查找树及测试
你需要知道的 TCP 四次挥手
欧拉系统(euleros):升级Mysql
C字符串数组反转
如何编辑epub电子书的目录
链式编程、包、访问权限
现代企业架构框架1
IDEA无法识别module(module右下角没有蓝色小方块)
MYSQL查询截取优化分析
High dimensional Gaussian distribution basics
RTL8762DK 点灯/LED(三)
RTL8762DK 使用DebugAnalyzer(四)
The kernel of the decompression process steps
WebApi hits an Attribute to handle exceptions uniformly
软考高级系统架构设计师系列之:信息系统基础知识
SC7A20 (Silan Micro-Accelerometer) Example
MYSQL逻辑架构
元宇宙改变人类工作模式的四种方式
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 "*"
软考高级系统架构设计师系列之:系统开发基础知识