#include <bits/stdc++.h>
using
namespace
std;
int
compare(
const
void
* ap,
const
void
* bp)
{
const
int
* a = (
int
*)ap;
const
int
* b = (
int
*)bp;
if
(*a < *b)
return
-1;
else
if
(*a > *b)
return
1;
else
return
0;
}
int
main()
{
int
arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int
ARR_SIZE =
sizeof
(arr) /
sizeof
(arr[0]);
int
key1 = 4;
int
* p1 = (
int
*)
bsearch
(&key1, arr, ARR_SIZE,
sizeof
(arr[0]), compare);
if
(p1)
cout << key1 <<
" found at position "
<< (p1 - arr)
<<
'\n'
;
else
cout << key1 <<
" not found\n"
;
int
key2 = 9;
int
* p2 = (
int
*)
bsearch
(&key2, arr, ARR_SIZE,
sizeof
(arr[0]), compare);
if
(p2)
cout << key2 <<
" found at position "
<< (p2 - arr)
<<
'\n'
;
else
cout << key2 <<
" not found\n"
;
}