Jump to content

Bogosort

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 24.60.20.159 (talk) at 23:50, 5 October 2002. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

Bogosort: According to the Jargon file, it is "the archetypal perversely awful algorithm." and it is "equivalent to repeatedly throwing a deck of cards in the air, picking them up at random, and then testing whether they are in order." C code snippet follows:

void bogo_sort (int *A) {

 int i, j;
 srand (time(0));
 while (!sorted(A))
 {
   B = make_visited (length(A));
   for (i = 0; i < length(A); ++i)
    {
       j = rand () % length (A);
       if (!B[j])
        {
          A[i] = A[j];
          B[j]++;
        }
    }
 }

}

int *make_visited (int k) {

 int i, *C = malloc (k*sizeof(int));
 for (i = 0; i < k; ++i)
   C[i] = 0;
 return C;

}

int sorted (int *A) {

 int i; 
 for (i = 0; i < length(A); ++i)
   if (A[i] > A[i+1])
     return 0;
 return 1;

}

References

the Jargon File 4.1.0