Elephant in Cairo
Encyclopedia
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:
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:
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.
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
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
Byte (magazine)
BYTE magazine was a microcomputer magazine, influential in the late 1970s and throughout the 1980s because of its wide-ranging editorial coverage...
magazine in September 1989 that described how various professions would go about hunting elephants, with programmers following the algorithm:
-
- Go to AfricaAfricaAfrica is the world's second largest and second most populous continent, after Asia. At about 30.2 million km² including adjacent islands, it covers 6% of the Earth's total surface area and 20.4% of the total land area...
. - Start at the Cape of Good HopeCape of Good HopeThe Cape of Good Hope is a rocky headland on the Atlantic coast of the Cape Peninsula, South Africa.There is a misconception that the Cape of Good Hope is the southern tip of Africa, because it was once believed to be the dividing point between the Atlantic and Indian Oceans. In fact, the...
. - 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.
- Go to Africa
Experienced programmers modify the above algorithm by placing a known elephant in Cairo
Cairo
Cairo , is the capital of Egypt and the largest city in the Arab world and Africa, and the 16th largest metropolitan area in the world. Nicknamed "The City of a Thousand Minarets" for its preponderance of Islamic architecture, Cairo has long been a centre of the region's political and cultural life...
to ensure that the algorithm will terminate. In technical terms, they use the elephant in Cairo as a sentinel value
Sentinel value
In computer programming, a sentinel value is a special value whose presence guarantees termination of a loop that processes structured data...
. The modified algorithm is therefore as follows:
-
- Go to AfricaAfricaAfrica is the world's second largest and second most populous continent, after Asia. At about 30.2 million km² including adjacent islands, it covers 6% of the Earth's total surface area and 20.4% of the total land area...
. - Put an elephant in CairoCairoCairo , is the capital of Egypt and the largest city in the Arab world and Africa, and the 16th largest metropolitan area in the world. Nicknamed "The City of a Thousand Minarets" for its preponderance of Islamic architecture, Cairo has long been a centre of the region's political and cultural life...
. - Start at the Cape of Good HopeCape of Good HopeThe Cape of Good Hope is a rocky headland on the Atlantic coast of the Cape Peninsula, South Africa.There is a misconception that the Cape of Good Hope is the southern tip of Africa, because it was once believed to be the dividing point between the Atlantic and Indian Oceans. In fact, the...
. - 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).
- Go to Africa
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.
External links
- Transcript of Stop Bit, Byte magazine, September 1989, p.404
- List of the other elephant hunting techniques (PDF)