int32 BinSearch(arrayp, n, elmtSize, key, compareProcP);
Searches an array of an arbitrary data type using the binary search algorithm. In addition to passing the array you want to search to this routine, you also pass a comparison procedure that this sort routine then uses to compare elements in the array.
The comparison routine should return a number less than zero if a is less than b, zero if a is equal to b, and a number greater than zero if a is greater than b.
You should declare the comparison routine to have the following parameters and return type:
int32 compareProcP(UPtr a, UPtr b);
Name | Type | Description |
---|---|---|
arrayp | UPtr | Pointer to an array of data. |
n | int32 | Number of elements in the array you want to search. |
elmtSize | int32 | Size in bytes of an array element. |
key | UPtr | Pointer to the data for which you want to search. |
compareProcP | ProcPtr | Comparison routine you want BinSearch to use to compare array elements. BinSearch passes this routine the addresses of two elements that it needs to compare. |
The position in the array where the data is found, with 0 being the first element of the array, if it is found. If the data is not found, BinSearch returns –i–1, where i is the position where key should be placed.