Programming Example: Binary Search
In this section we will illustrate some of LINGO’s programming features by developing a model to perform a binary search. Binary searches are an efficient way to look up a key in a sorted list. In the worst case, a binary search should perform no more than log2( n) comparisons to determine if a key is on a sorted list, where n is the number of items on the list.
The basic idea behind a binary search is that you bracket the key in the list. You then iterate by reducing the size of the bracket by a factor of 2 each pass. This process continues until you either find the key, or conclude that the key is not on the list when the bracket size becomes 0.
Note: | A binary search is not typically something you would do in LINGO, and we use it here merely as an example of a simple algorithm for illustrative purposes. |