#include<iostream>
using namespace std;
int a[100005];
int n;
void recur(int a[], int left, int right)
{
if (left >= right)
return;
int pivot = a[(left + right) / 2];
int temp = (left + right) / 2;
int L = left, R = right;
while (L < R)
{
while (a[R] >= pivot && R > temp)
R--;
if (R > temp)
{
a[temp] = a[R];
temp = R;
}
//cout << L << " " << R << endl;
/*for (int i = 0; i < 30; i++)
if(i!=temp)
cout << a[i] << ' ';
else cout << '@'<<' ';
cout << endl;*/
//cout << temp << endl;
while (a[L] <= pivot && L < temp)
L++;
if (L < temp)
{
a[temp] = a[L];
temp = L;
}
//cout << L << " " << R << endl;
/*for (int i = 0; i < 30; i++)
if(i!=temp)
cout << a[i] << ' ';
else cout << '@'<<' ';
cout << endl;*/
//cout << temp << endl;
if (L >= R)
{
a[temp] = pivot;
break;
}
}
//cout << left << ' ' << temp - 1 << endl;
recur(a, left, temp - 1);
//cout << temp + 1 << ' ' << right<< endl;
recur(a, temp + 1, right);
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
recur(a, 0, n - 1);
for (int i = 0; i < n - 1; i++)
cout << a[i] << ' ';
cout << a[n - 1];
cout << endl;
return 0;
}
01-25
467
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-04
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交