Bogosort
Appearance
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;
}