Searching is a task closely related to sorting. The basic idea is to quickly locate a specific value in an array or determine that the array doesn't contain the value. Although searching for a particular value in an array of simple data (e.g., characters or integers) has limited use, doing so is a good example of a searching algorithm. Searching for a specific element becomes more useful when the elements are objects, instances of structures, or classes.
Imagine that we begin with a student structure that has three fields or members: (1) the student's name, (2) the student's identification number, and (3) the student's GPA. Next, we create an array of objects instantiated from the structure and initialize all fields in each object. Now, we can search for a specific student's ID; if we find that ID, the search also returns the student's name and GPA. This operation is called an associative search, and the structure field used for the search, ID in this example, is called the key.
Binary search is a simple but effective searching algorithm often used with arrays. However, it has one requirement: we must sort the array before we can apply the binary search algorithm. The basic operation is to divide the array in half and ask, "Is the element I'm looking for in the top or bottom half?" Ignore the half of the array that doesn't contain the element, and then repeat the process until the algorithm locates the correct element or determines that the element is not in the array. The binary search doesn't change the array; instead, it performs its operations through three variables that index into the array.
|  |  |  |  |  | 
| (a) | (b) | (c) | (d) | (e) | 
#include <iostream>
using namespace std;
void ssort(char* data, int size);
int  binsearch(char key, char* data, int size);
int main()
{
	char	data[] = { 'S','E','E','T','H','E','Q','U','I',		// (a)
			'C','K','R','E','D','F','O','X' };
	int	size = sizeof(data) / sizeof(char);			// (b)
	ssort(data, size);						// (c)
	int index = binsearch('Q', data, size);				// (d)
	if (index >= 0)							// (e)
		cout << "key found at index = " << index << endl;
	else
		cout << "key not in array" << endl;
	return 0;
}
int binsearch(char key, char* data, int size)
{
	int top = 0;					// (a)
	int bottom = size - 1;				// (b)
	do						// (c)
	{
		int mid = (top + bottom) / 2;		// (d)
		if (key < data[mid])
			bottom = mid - 1;		// (e)
		else if (key > data[mid])
			top = mid + 1;			// (f)
		else
			return mid;			// (g)
	} while (top <= bottom);			// (h)
	return -1;					// (i)
}
#include <iostream>
using namespace std;
struct student
{	char	*name;
	int	id;
	double	gpa;
};
void ssort(student* data, int size);
int  binsearch(int key, student* data, int size);
int main()
{
	student students[] = {
			{ "Dilbert", 123, 3.5 },
			{ "Wally", 456, 2.0 },
			{ "Alice", 987, 3.9 },
			{ "Asok", 730, 3.8 },
			{ "Catbert", 501, 3.0 },
			{ "Pointy Haired Boss", 666, 1.0 },
			{ "Dogbert", 111, 4.0 }
		};
	int	size = sizeof(students) / sizeof(student);
	ssort(students, size);				// array must be sorted for binary search
	int index = binsearch(501, students, size);	// call binary search
	if (index >= 0)
		cout << "key found at index = " << index << endl;
	else
		cout << "key not in array" << endl;
	return 0;
}
int binsearch(int key, student* data, int size)
{
	int top = 0;
	int bottom = size - 1;
	do
	{
		int mid = (top + bottom) / 2;
		if (key < data[mid].id)
			bottom = mid - 1;		// search top half of sub-array
		else if (key > data[mid].id)
			top = mid + 1;			// search bottom half of sub-array
		else
			return mid;			// key found
	} while (top <= bottom);
	return -1;					// key not in the data array
}
| View | Download | 
|---|---|
| binsearch.cpp (simple data) | binsearch.cpp (simple data) | 
| binsearch2.cpp (object) | binsearch2.cpp (object) |