#include <stdio.h>
int* binary_search( int* a, int* b, int n )
{
int* m;
while ( b > a ) {
m = a + ( b - a ) / 2;
if ( *m < n )
a = m + 1;
else if ( *m > n )
b = m;
else
return m;
}
return NULL;
}
int main()
{
int a[] = { 1,3,5,7,9,11,13,15,17,19 };
int* p1, *p2;
p1 = binary_search( a, a + 10, 9 );
p2 = binary_search( a, a + 10, 8 );
if ( p1 ) {
printf( "find %d at pos:%d\n", 9, p1 - a );
}
if ( !p2 ) {
printf( "can't find value %d\n", 8 );
}
return 0;
}
2019-07-17 22:55:34