经典问题,话不多说,直接上代码
#include <stdio.h>
#include <string.h>
void fast(long long int x,long long int y);
long long int num[500010];
int main(int argc, char const *argv[])
{
long long int tot;
scanf("%d",&tot);
for (int i = 0; i < tot; ++i) scanf("%lld",&num[i]);
printf("\n");
fast(0,tot-1); //进入
for (int i = 0; i < tot; ++i) printf("%lld ",num[i]);
return 0;
}
void fast(long long int x,long long int y)
{
if( x<y )
{
int L = x, R = y;
long long int cur = num[L];
while(L < R )
{
while(num[R] > cur && L<R)
R--;
if(L != R)
num[L++] = num[R]; //在右边寻找大数
while(num[L] < cur && L<R)
L++;
if(L != R)
num[R--] = num[L]; // 在左边寻找小数
}
num[L] = cur; // 确定分界值
fast(x,L-1);
fast(L+1,y); // 两边划分
}
else
return;
}