#include <iostream>
using namespace std;
const int N = 1e6 + 10;
int n;
int q[N];
void quick_sort(int q[], int l, int r)
{
// l与r之间没有或者只有一个数时就退出
if (l >= r) return;
// 取数组两边的数,因为每次先往中间移动一次
int x = q[l], i = l - 1, j = r + 1;
while (l < j)
{
do i++; while (q[i] < x);
do j--; while (q[j] > x);
if (i < j) //swap(q[i], q[j]);
{
int tmp = 0;
tmp = q[i];
q[i] = q[j];
q[j] = tmp;
}
}
quick_sort(q, l, j);
quick_sort(q, j + 1 , r);
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%d", &q[N]);
quick_sort(q, 0, n - 1);
for (int i = 0; i < n; i ++ ) printf("%d", q[N]);
return 0;
}
快速排序
于 2023-03-27 01:07:31 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)