当前位置:网站首页>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=' ')
边栏推荐
- 你需要知道的 TCP 四次挥手
- 【Cryptography/Cryptanalysis】Cryptanalysis method based on TMTO
- MYSQL logical architecture
- Google engineer fired for claiming AI awareness: breach of nondisclosure agreement
- 数据中台建设(七):数据资产管理
- OSD读取SAP CRM One Order应用日志的优化方式
- Daily practice of LeetCode - Circular linked list question (interview four consecutive questions)
- 机器学习应该如何入门?
- Item 36: Specify std::launch::async if asynchronicity is essential.
- C string array reverse
猜你喜欢

Beijing suddenly announced that yuan universe big news

MYSQL Keyword Explain Analysis

ROS2系列知识(4): 理解【服务】的概念

ARM 交叉编译

IDEA 找不到或无法加载主类 或 Module “*“ must not contain source root “*“ The root already belongs to module “*“

STK8321 I2C(昇佳-加速度传感器)示例

Blueprint: Yang Hui's Triangular Arrangement

机器学习初学者可以学哪些实战项目?

MYSQL关键字Explain解析

Cmake introductory study notes
随机推荐
Chinese version of Pylint inspection rules
MYSQL two-phase commit
Cmake introductory study notes
The device node structure is converted into a platform_device structure
OSF一分钟了解敏捷开发模式
RTL8762DK uses DebugAnalyzer (four)
Chain programming, packages, access
Handwritten binary search tree and test
cmake入门学习笔记
Introduction to the decision logic of WAASAP WebClient UI page labels
Item 36: Specify std::launch::async if asynchronicity is essential.
彻底关闭Chrome浏览器更新及右上角的更新提示
IDEA无法识别module(module右下角没有蓝色小方块)
Soft Exam Senior System Architect Series: Basic Knowledge of System Development
Google engineer fired for claiming AI awareness: breach of nondisclosure agreement
Device tree - conversion from dtb format to struct device node structure
手写二叉查找树及测试
MYSQL logical architecture
Daily practice of LeetCode - Circular linked list question (interview four consecutive questions)
Detailed explanation of TCP protocol