Bogorithms version 1.0 Fundametal Routines for the Professional Programmer _____________________________________________________________________ Benjamin Smith-Mannschott 93.11.24.11.32.53 BogoSort BogoSort is a favorite among professional programmers, for it's simplicity and ease of use. The algorithm can be adapted easily for sorting both arrays and linked lists. Unlike many other sorting algorthims, It's execution shows a consistant character regardless of the representation of the data to be sorted. BogoSort works on the exceedingly simple Exchange and Check principle, discovered, quite by accident, by the creator of Ada while drinking sorting a deck of cards. It occured to him, at three in the morning, that the simplest way to sort the deck of cards in his hands was to shuffle the cards and then check to see if the deck is in order. BogoSort is based on this simply understood principle, although, it reduces shuffling to a minimum to promote a more efficient solution. Traditionally, BogoSort is implemented in a non-tail recursive fashion. It's creator, a strong believer in "use it, or loose it", felt that this would give the call stack of the machine running BogoSort some much needed exercise. For simplicity, and readability, the BogoSort code below, is written to sort an array, but as mentioned above, it can be adapted quite easily for the sorting of linked lists. PROCEDURE BogoSort(VAR AnArray: ARRAY OF INTEGER); VAR a, b, temp: INTEGER; PROCEDURE InOrder(): BOOLEAN; (* checks to see if the array is in order from least to greatest *) VAR i: INTEGER; BEGIN FOR i := 0 TO LEN(AnArray)-2 DO IF AnArray[i] > AnArray[i+1] THEN RETURN FALSE END END; RETURN TRUE; END InOrder; BEGIN a := RandomNumber.Dice(LEN(AnArray)); b := RandomNumber.Dice(LEN(AnArray)); temp := AnArray[a]; AnArray[a] := AnArray[b]; AnArray[b] := temp; IF ~InOrder() THEN BogoSort(AnArray) END END BogoSort; BogoSearch BogoSearch is another favorite especially among developers of large, government-contracted, data base engines. BogoSearch reduces the computational overhead of sorting so often associated with the management of a large data base, because it does not require the list to be sorted in any way! BogoSearch is rumored to be used by the IRS to keep track of the millions of honest tax payers in this country. BogoSearch works on the "Poke it and see if it squeels" principle, often used when checking the metabolic status of pigs. But, that's another story. Given an array, and a key to search for, this implementation of BogoSearch will return the array index of the element with the requested key. As can be seen from the source code listed below, BogoSearch packs an incredibly functional procedure into an absolute minimum of code, significantly shortening development time. PROCEDURE BogoSearch(AnArray: ARRAY OF INTEGER; Key: INTEGER): INTEGER; VAR i : INTEGER; BEGIN LOOP i := RandomNumber.Dice(LEN(AnArray)); IF AnArray[i] = Key THEN RETURN i; END; END BogoSearch; BogoHash BogoHash is the final useful routine from the Bogorithm library. It is the only Hashing algorithm in the world, which does not require a hash key to generate the array subscript of the element to be stored. This eliminates the stack overhead caused by passing the hash key from procedure to procedure, and also frees the programmer of the difficult taks of deciding which field of the record should be used as the hash key. The code sample below, shows BogoHash working its magic on an array of pointers to Integers. This version, checks to see if the "best-guess" index it generates has already been allocated to store a piece of data. If greater speed it desired, this saftey check can be removed. PROCEDURE BogoHash(VAR AnArray: ARRAY OF POINTER TO INTEGER; Allocate: BOOLEAN): INTEGER; (* returns the array index *) VAR i : INTEGER; BEGIN LOOP i := RandomNumber.Dice(LEN(AnArray)); IF AnArray[i] = NIL THEN IF Allocate THEN NEW(AnArray[i]) END; RETURN i END END; END BogoHash; PROCEDURE BogoHashFast(VAR AnArray: ARRAY OF POINTER TO INTEGER; Allocate: BOOLEAN): INTEGER; BEGIN i := RandomNumber.Dice(LEN(AnArray)); IF Allocate THEN NEW(AnArray[i]) END; RETURN i; END BogoHashFast; Don't Forget... This is just a sample of the expansive array of possibilities that awaits the programmer who embraces the Bogo Philosophy. Just remember, if it isn't random, it's not good enough!