ms很多人都超时了……
韶华易逝,光阴如梭,Debug这种浪费青春浪费生命的事情还是少做为好。。
写的很渣,只是想给大家一个能过的代码,求轻喷~~~(以及别直接Copy啊亲们>﹏<)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define MAXLEN 100010
int Data[MAXLEN];
/* 三个数组,分别存放小于、等于、大于key的数 */
int tmp_l[MAXLEN], tmp_m[MAXLEN], tmp_r[MAXLEN];
void Quick_Sort(int *A, int len) {
if (len <= 1)
return;
/* 随机生成一个key */
int key = A[(rand() % (len - 1)) + 1];
int i = 0, j = 0, k = 0;
for (int iter = 0; iter < len; iter++) {
if (A[iter] < key)
tmp_l[i++] = A[iter];
else if (A[iter] == key)
tmp_m[j++] = A[iter];
else
tmp_r[k++] = A[iter];
}
memcpy(A, tmp_l, sizeof(int)*i);
memcpy(A + i, tmp_m, sizeof(int)*j);
memcpy(A + i + j, tmp_r, sizeof(int)*k);
Quick_Sort(A, i);
Quick_Sort(A + i + j, k);
}
int main() {
int N; scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%d", &Data[i]);
srand(time(NULL));
Quick_Sort(Data, N);
for (int i = 0; i < N; i++)
printf("%d\n", Data[i]);
return 0;
}