This just might be the world's worst sorting algorithm. It is similar to trying to sort a deck of cards by shuffling them and then checking to see if they are in order. The source for the bogosort function is here. The source for main is here. If you want to build it, use this command.
[daver@tombstone:~/src/c/bogosort]$ cc main.c bogosort.c -o main
To use the program, give it a quoted string as an argument. It works something like this.
[daver@tombstone:~/src/c/bogosort]$ ./main "foo bar" Size of list: 7 Before sort: foo bar After sort: abfoor Number of iterations: 1789 [daver@tombstone:~/src/c/bogosort]$
Bogosort has a terrible time dealing with anything but really small arrays. Lets take a look at how long it takes to sort a reasonably small array on my Athlon box.
[daver@tombstone:~/src/c/bogosort]$ dmesg | grep ^CPU CPU: AMD-K7(tm) Processor (548.94-MHz 686-class CPU) [daver@tombstone:~/src/c/bogosort]$ time ./main "christoffel" Size of list: 11 Before sort: christoffel After sort: ceffhilorst Number of iterations: 19962198 real 0m46.992s user 0m43.155s sys 0m0.110s [daver@tombstone:~/src/c/bogosort]$
Ew... I hope that no one thinks this is a good idea. Remember that we have no way of determining how long the algorithm is going to run. There is a really small chance that it could sort the list on its first iteration, but this is almost as rare as Ethan going to sleep the night before a qLab writeup is due.