# BinSearch

int BinSearch (const void *arrayToSearch, int numberOfElements, int elementSize, const void *itemPointer, CompareFunction comparisonFunction);

## Purpose

Performs a binary search in an array of numberOfElements items of size elementSize.

Important: The binary search algorithm assumes that the array is already sorted in ascending order.

This function is like the C Library bsearch function except that this function returns the index where the item should be placed if it is not found.

## Parameters

Input
Name | Type | Description
arrayToSearch | const void * | Pass the array of items that BinSearch will search. Important: The binary search algorithm assumes that the array is already sorted in ascending order.
numberOfElements | integer | Pass the number of items in the array.
elementSize | integer | Pass the item size (in bytes) for the items in the array.
itemPointer | const void * | Pass a pointer to the item to search for in the array.
comparisonFunction | CompareFunction | Pass a comparison function that BinSearch should use to compare items in the array to the item pointed to by itemPointer. The comparison function should have the following prototype: int CVICALLBACK CompareFunction (void *item1, void *item2); The comparison function should return a negative number if item1 is less than item2, it should return 0 if item1 is equal to item2, and it should return a positive number if item1 is greater than item2. When the comparison function is called, item1 is always the item pointer passed to BinSearch and item2 is always a pointer to an element in the array passed to BinSearch. This instrument driver provides several commonly useful comparison functions: ShortCompare IntCompare FloatCompare DoubleCompare CStringCompare CStringNoCaseCompare

## Return Value

Name | Type | Description
result | integer | Returns the index of the array element that matches the item (from 0 to numberOfElements – 1). If no match is found, it returns where the item should be placed if it were to be inserted in order, –i – 1 where i is the index (0 to numElements).