Код на Java:
Код:
Random random = new Random();
public void bogoSort(int[] n) {
while(!inOrder(n))shuffle(n);
}
public void shuffle(int[] n) {
for (int i = 0; i < n.length; i++) {
int swapPosition = random.nextInt(i + 1);
int temp = n[i];
n[i] = n[swapPosition];
n[swapPosition] = temp;
}
}
public boolean inOrder(int[] n) {
for (int i = 0; i < n.length-1; i++) {
if (n[i] > n[i+1]) return false;
}
return true;
}
Мой код на C:
Код:
#define na 10
int a[na];
void bogoSort(int *item, int count);
int main(void)
{
int i;
srand(time(NULL));
for(i=0; i<=na; i++) a[i]=rand()%100-50;
printf("a[]={");
for(i=0; i<=na; i++) printf("%d ", a[i]);
printf("}\n");
bogoSort(a, na);
printf("a[]={");
for(i=0; i<=na; i++) printf("%d ", a[i]);
printf("}\n");
return 0;
}
void shuffle(int *item, int count)
{
register int i, swapPosition, temp;
for(i=0; i<count; i++)
{
swapPosition=rand()%i+1;
temp=item[i];
item[i]=item[swapPosition];
item[swapPosition]=temp;
}
}
_Bool inOrder(int *item, int count)
{
int i;
for (i=0; i<count-1; i++)
{
if(item[i]>item[i+1]) return 0;
}
return 1;
}
void bogoSort(int *item, int count)
{
while(!inOrder(item, count)) shuffle(item, count);
}
Что не так?