|
|
|
|
@ -0,0 +1,47 @@
|
|
|
|
|
快速排序用在算法中,它的时间复杂度为O(nlogn),效率比较高;
|
|
|
|
|
例子:
|
|
|
|
|
给定你一个长度为 [Math Processing Error] 的整数数列。
|
|
|
|
|
请你使用快速排序对这个数列按照从小到大进行排序。
|
|
|
|
|
并将排好序的数列按顺序输出。
|
|
|
|
|
输入格式
|
|
|
|
|
输入共两行,第一行包含整数 [Math Processing Error]。
|
|
|
|
|
第二行包含 [Math Processing Error] 个整数(所有整数均在 [Math Processing Error] 范围内),表示整个数列。
|
|
|
|
|
输出格式
|
|
|
|
|
输出共一行,包含 [Math Processing Error] 个整数,表示排好序的数列。
|
|
|
|
|
数据范围
|
|
|
|
|
[Math Processing Error]
|
|
|
|
|
输入样例:
|
|
|
|
|
5
|
|
|
|
|
3 1 2 4 5
|
|
|
|
|
代码实现:
|
|
|
|
|
#include<iostream>
|
|
|
|
|
using namespace std;
|
|
|
|
|
const int N = 1e5 + 10;
|
|
|
|
|
int q[N];
|
|
|
|
|
void quick_sort(int l, int r)
|
|
|
|
|
{
|
|
|
|
|
if(l >= r)
|
|
|
|
|
return ;
|
|
|
|
|
int x = q[(l + r) / 2];
|
|
|
|
|
int i = l - 1, j = r + 1;
|
|
|
|
|
while(i<j)
|
|
|
|
|
{
|
|
|
|
|
while(q[++i] < x);
|
|
|
|
|
while(q[--j] > x);
|
|
|
|
|
if(i<j)
|
|
|
|
|
swap(q[i],q[j]);
|
|
|
|
|
}
|
|
|
|
|
quick_sort(l, j);
|
|
|
|
|
quick_sort(j + 1, r);
|
|
|
|
|
}
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
int n;
|
|
|
|
|
cin >> n;
|
|
|
|
|
for(int i = 0; i < n; i++)
|
|
|
|
|
cin >> q[i];
|
|
|
|
|
quick_sort(0,n - 1);
|
|
|
|
|
for(int i = 0;i < n; i++)
|
|
|
|
|
cout << q[i] << ' ';//<<' '
|
|
|
|
|
return 0;
|
|
|
|
|
}
|