快多了~~#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;
index = rand() % (length);
if (index)
{
temp = array[0];
array[0] = array[index];
array[index] = temp;
}
}
void bogo_sort(int array[], int length)
{
srand(0);
while (!in_order(array, length))
{
shuffle(array, length);
}
}
void print(int arr[], int n)
{
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
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("排序后的结果是:");
print(array, length);
end = time(NULL);
printf("总共耗时:%0.4lf秒\n", (double)(end - begin)/CLOCKS_PER_SEC);
return 0;
}
|