64,651
社区成员
发帖
与我相关
我的任务
分享
#include <time.h>
#include <stdio.h>
//N超过65532之后排序时间从0变成9秒
#define N 65535
int *p = new int[N];
int main()
{
time_t t0,t1,t2;
time(&t0);
unsigned i = 0;
//把数组按下标从小到大顺序赋值,即数组中前面的小于后面
do
{
p[i] = i;
++i;
} while (i < N);
time(&t1);
double cost = difftime(t1, t0);
printf("%f\n",cost);
bool chang = true;
for (unsigned i = N; i >= 1 && chang; --i)
{
chang = false;
for (unsigned j = 0; j < i; ++j)
if(p[j] > p[j+1]) //判断是否前面小于后面 ‘<’,前面小于后面 ‘>’
{
int temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
chang = true;
}
}
time(&t2);
cost = difftime(t2, t1);
printf("%f\n",cost);
return 0;
}