当前位置:网站首页>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=' ')
边栏推荐
- date command
- Rasa 3.x 学习系列- Rasa - Issues 4898 学习笔记
- JQESAP系统里的胖接口Fat interface
- 设备树的树形结构到底是怎样体现的?
- STK8321 I2C (Shengjia-accelerometer) example
- 内核的解压缩过程详解
- 从零造键盘的键盘超级喜欢,IT人最爱
- Beijing suddenly announced that yuan universe big news
- Google Earth Engine - Error resolution of Error: Image.clipToBoundsAndScale, argument 'input': Invalid type
- 软考高级系统架构设计师系列之:信息系统基础知识
猜你喜欢

Daily practice of LeetCode - Circular linked list question (interview four consecutive questions)
![ROS2 series of knowledge (4): understand the concept of [service]](/img/14/8de92a89d9c4b6476ac37408bc7788.png)
ROS2 series of knowledge (4): understand the concept of [service]

IDEA debugging

RTL8762DK Lighting/LED (3)

Four ways the Metaverse is changing the way humans work

RTL8762DK UART(二)

普通用户无法访问hgfs目录

MYSQL关键字Explain解析

STK8321 I2C (Shengjia-accelerometer) example

被 CSDN,伤透了心
随机推荐
普通用户无法访问hgfs目录
Introduction to the decision logic of WAASAP WebClient UI page labels
Beijing suddenly announced that yuan universe big news
高维高斯分布基础
ROS2系列知识(4): 理解【服务】的概念
Nmap 操作手册 - 完整版
RTL8762DK WDG (six)
[Data analysis] Based on matlab GUI student achievement management system [including Matlab source code 1981]
Item 36: Specify std::launch::async if asynchronicity is essential.
OSD read SAP CRM One Order application log way of optimization
Key Points Estimation and Point Instance
IDEA modifies the annotation font
Rasa 3.x 学习系列-使用查找表改进实体提取
如何编辑epub电子书的目录
WeChat applet page syntax
IDEA debugging
元宇宙改变人类工作模式的四种方式
The device node structure is converted into a platform_device structure
Simple vim configuration
Summary of JVM interview questions (continuously updated)