当前位置:网站首页>Acwing785. 快速排序
Acwing785. 快速排序
2022-07-17 16:28:00 【理工大猪猪】
给定你一个长度为 n 的整数数列。
请你使用快速排序对这个数列按照从小到大进行排序。
并将排好序的数列按顺序输出。
输入格式
输入共两行,第一行包含整数 n。
第二行包含 n 个整数(所有整数均在 1∼10^9 范围内),表示整个数列。
输出格式
输出共一行,包含 n 个整数,表示排好序的数列。
数据范围
1≤n≤100000
输入样例:
5
3 1 2 4 5
输出样例:
1 2 3 4 5
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int q[N];
void quick_sort(int q[], int l, int r)
{
if (l >= r) return;
int i = l - 1, j = r + 1, x = q[l + r >> 1];
while (i < j)
{
do {
i ++;
}while (q[i] < x);
do{
j -- ;
}while (q[j] > x);
if (i < j) swap(q[i], q[j]);
}
quick_sort(q, l, j);
quick_sort(q, j + 1, r);
}
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
quick_sort(q, 0, n - 1);
for (int i = 0; i < n; i ++ ) printf("%d ", q[i]);
return 0;
}
边栏推荐
- C#从入门到精通之第一篇: C#概述与入门
- hcip第二天笔记
- In 2022, how to choose cross end technology solutions?
- Simple implementation of scrapy keyword crawler (take Xinhuanet and people's network as examples)
- Machine learning Assignment 1
- GET 请求和 POST 请求的区别与使用示例
- Nature子刊 | 地下水固碳速率与寡营养海洋系统固碳速率相近
- SQL盲注详解
- 机器学习作业1
- 如何应用小程序容器技术开发Hybrid App
猜你喜欢
随机推荐
C# .NET 云南农信国密签名(SM2)简要解析
机器学习作业1
Machine learning Assignment 1
Es install IK word breaker
微机原理与技术接口 实验四 子程序及中断实验
S32K148_CAN驱动(裸机开发)
Test whether the time-consuming of vector, list and set calling empty and size is constant
Solution: function RGB is missing argument $green Problems of
HCIP(8)
2022年低压电工考试题及在线模拟考试
Core base station_ The error "no gateways configured" is reported when starting the CPA file
Notes on the fifth day
mycat 根据字符串范围hash值分库分表
MySQL learning notes - paging - table creation - data type
Pytorch version: yolov4 integrating attention and mobilenet
mysql如何删除数据表,被关联的数据表如何删除呢
熟悉NestJS (新手篇)
谷歌开发者社区分享——Flutter动画的分享已发布
Leetcode 239. 滑动窗口最大值
mysql数据库表增添字段,删除字段、修改字段的排列等操作,还不快来