Mastering Algorithms With C - Kyle Loudon [143]
Figure 12.8. Searching for 47 using binary search
The runtime complexity of binary search depends on the maximum number of divisions possible during the searching process. For a set of n elements, we can perform up to lg n divisions. For binary search, this represents the number of inspections that we could end up performing in the worst case: when the target is not found, for example. Therefore, the runtime complexity of binary search is O (lg n).
Example 12.8. Implementation of Binary Search
/*****************************************************************************
* *
* ------------------------------ bisearch.c ------------------------------ *
* *
*****************************************************************************/
#include #include #include "search.h" /***************************************************************************** * * * ------------------------------- bisearch ------------------------------- * * * *****************************************************************************/ int bisearch(void *sorted, const void *target, int size, int esize, int (*compare)(const void *key1, const void *key2)) { int left, middle, right; /***************************************************************************** * * * Continue searching until the left and right indices cross. * * * *****************************************************************************/ left = 0; right = size - 1; while (left <= right) { middle = (left + right) / 2; switch (compare(((char *)sorted + (esize * middle)), target)) { case -1: /*********************************************************************** * * * Prepare to search to the right of the middle index. * * * ***********************************************************************/ left = middle + 1; break; case 1: /*********************************************************************** * * * Prepare to search to the left of the middle index. * * * ***********************************************************************/ right = middle - 1; break; case 0: /*********************************************************************** * * * Return the exact index where the data has been found. * * * ***********************************************************************/ return middle; } } /***************************************************************************** * * * Return that the data was not found. * * * *****************************************************************************/ return -1; } Binary Search Example: Spell Checking The example presented here consists of a function, spell (see Examples Example 12.9 and Example 12.10), that checks the spelling of words from a string of text one word at a time. The function accepts three arguments: dictionary is a sorted array of acceptable strings, size is the number of strings in the dictionary, and word is the word to check. The function calls bisearch to look up word in dictionary. If it finds the word,
Using spell checkers has become an expected part of preparing all types of documents. From a computing standpoint, a basic spell checker works simply by checking words in a string of text against a dictionary. The dictionary contains the set of acceptable words.