Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <class elemType>
- int binarySearch(const elemType list[], int length,
- int& comparisons, const elemType& searchItem)
- {
- // TODO
- int mid = -1;
- int min = 0;
- int max = length - 1;
- while (max >= min)
- {
- mid = (max + min) / 2;
- comparisons++;
- if (searchItem == list[mid]) // if value is found, return the position
- {
- return mid;
- }
- else if (searchItem < list[mid])
- {
- comparisons++;
- max = mid - 1;
- }
- else
- {
- comparisons++;
- min = mid + 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement