s1e32
猴子排序为什么我这总是零秒就跑完了?虽然可以打印排序过程拖慢时间,是哪里除了问题?
#include <stdio.h>
#include <stdlib.h>
void bogo_sort(int *, int);
int main(void)
{
int array[] = {73, 108, 111, 118, 101, 70, 105, 104, 67};
int i, length;
time_t begin, end;
begin = time(NULL);
length = sizeof(array) / sizeof(array);
bogo_sort(array, length);
printf("排序后的结果是:");
for (i = 0; i < length; i++)
{
printf("%d ", array);
}
putchar('\n');
end = time(NULL);
printf("总共耗时:%ld秒\n", end - begin);
return 0;
}
void bogo_sort(int array[], int length)
{
int i, j;
int temp, index;
_Bool flag;
srand(time(NULL));
do
{
for (i = 0; i < length; i++)
{
index = rand() % length;
temp = array;
array = array;
array = temp;
}
for (i = 0, flag = 1; i < length && flag; i++)
{
for (j = i+1; j < length && flag; j++)
{
flag = array < array;
}
}
}while (!flag);
}
页:
[1]