0
点赞
收藏
分享

微信扫一扫

快排模板


#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;
}


举报

相关推荐

快排模板(超简单)

经典快排

快排+归并

链表快排

C++快排

排序思想-快排

归并和快排

js 实现快排

0 条评论