Elephant in Cairo
Encyclopedia
An elephant in Cairo is a term used in computer programming
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:
  1. Go to Africa
    Africa
    Africa 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...

    .
  2. Start at the Cape of Good Hope
    Cape of Good Hope
    The 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...

    .
  3. Work northward in an orderly manner, traversing the continent alternately east and west,
  4. During each traverse pass:
  5. 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
  6. 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
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:
  1. Go to Africa
    Africa
    Africa 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...

    .
  2. Put an 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...

    .
  3. Start at the Cape of Good Hope
    Cape of Good Hope
    The 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...

    .
  4. Work northward in an orderly manner, traversing the continent alternately east and west,
  5. During each traverse pass:
    • Catch each animal seen.
    • Compare each animal caught to a known elephant.
    • Stop when a match is detected.
  6. 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.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK