|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int in_order(int array[], int length);
void shuffle(int array[], int length);
void bogo_sort(int array[], int length);
int in_order(int array[], int length)
{
int i = 0;
while (array[i] <= array[i+1] && ++i < length - 1)
;
if (i == length - 1)
{
return 1;
}
else
{
return 0;
}
}
void shuffle(int array[], int length)
{
int index, temp, i;
static int t1, t2;
srand(t1);
t1 = rand();
t2 = time(NULL);
srand(t1+t2);
for (i = 0; i < length; i++)
{
index = rand() % (length - i) + i;
if (index != i)
{
temp = array[i];
array[i] = array[index];
array[index] = temp;
}
}
}
void bogo_sort(int array[], int length)
{
while (!in_order(array, length))
{
shuffle(array, length);
}
}
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[0]);
bogo_sort(array, length);
printf("排序后的结果是:");
for (i = 0; i < length; i++)
{
printf("%d ", array[i]);
}
putchar('\n');
end = time(NULL);
printf("总共耗时:%ld秒\n", end - begin);
return 0;
}
无法正常运行,打开没有东西,会陷入死循环!
程序目的是实现无限猴子定理,用随机的方法打乱数组,总有一次数组会按照从小到大的顺序排列。
、
严重怀疑问题出在红色语句
有执行 #include <stdio.h>
#include <stdlib.h>
#include <time.h>
int in_order(int array[], int length);
void shuffle(int array[], int length);
void bogo_sort(int array[], int length);
int in_order(int array[], int length)
{
int i = 0;
while (array[i] <= array[i+1] && ++i < length - 1)
;
if (i == length - 1)
{
return 1;
}
else
{
return 0;
}
}
void shuffle(int array[], int length)
{
int index, temp, i;
static int t1, t2;
srand(t1);
t1 = rand();
t2 = time(NULL);
srand(t1+t2);
for (i = 0; i < length; i++)
{
index = rand() % (length - i) + i;
if (index != i)
{
temp = array[i];
array[i] = array[index];
array[index] = temp;
}
}
}
void bogo_sort(int array[], int length)
{
int c = 0;
while (!in_order(array, length))
{
shuffle(array, length);
printf("%d\n",c++);
}
}
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[0]);
bogo_sort(array, length);
printf("排序后的结果是:");
for (i = 0; i < length; i++)
{
printf("%d ", array);
}
putchar('\n');
end = time(NULL);
printf("总共耗时:%0.4lf秒\n", (double)(end - begin)/CLOCKS_PER_SEC);
return 0;
}
|
|