Elephant in Cairo
| This article is an orphan, as few or no other articles link to it. Please introduce links to this page from related articles; suggestions may be available. (November 2008) |
|
|
This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. (June 2006) |
An elephant in Cairo is a term used in computer programming to describe a piece of data inserted at the end of a search space, which matches the search criteria, in order to make sure the search algorithm terminates. The term derives from a humorous essay circulated on the Internet and published in Byte magazine in September 1989 that described how various professions would go about hunting elephants, with programmers following the algorithm:
-
- Go to Africa.
- Start at the Cape of Good Hope.
- Work northward in an orderly manner, traversing the continent alternately east and west,
- During each traverse pass:
- Check whether we are at the end of Africa. If not:
-
- Catch each animal seen.
- Compare each animal caught to a known elephant.
- Stop when a match is detected
-
- If you are at the end of Africa, there are no elephants in Africa.
Experienced programmers modify the above algorithm by placing a known elephant in Cairo to ensure that the algorithm will terminate. In technical terms, they use the elephant in Cairo as a sentinel value. The modified algorithm is therefore as follows:
-
- Go to Africa.
- Put an elephant in Cairo.
- Start at the Cape of Good Hope.
- Work northward in an orderly manner, traversing the continent alternately east and west,
- During each traverse pass:
-
- Catch each animal seen.
- Compare each animal caught to a known elephant.
- Stop when a match is detected.
-
- If you are in Cairo, then there are no elephants in Africa (other than the one you placed there).
The removal of the requirement to check for the end of Africa each time speeds up the algorithm (although with modern processors implementing branch prediction, the speed-up is likely to be negligible) and simplifies the code.
[edit] See also
[edit] References
- Olsen, Peter C. "Pachydermic Personnel Prediction", Byte, Stop Bit column, September 1989.
[edit] External links
- Transcript of Stop Bit, Byte magazine, September 1989, p.404
- List of the other elephant hunting techniques (PDF)