二分法查找升序长度为len的数组a中的target元素
查找成功则返回下标
否则,返回-1
#include <stdio.h>
int print_num( int num )
{
printf( "print_num() num = %d\n", num );
return 0;
}
typedef int ( *type_print_num_func )( int num );
/*
二分法查找升序长度为len的数组a中的target元素
查找成功则返回下标
否则,返回-1
*/
int bsearch( int *a, int len, int target )
{
int low = 0, high = len, mid;
/*查找开始*/
while( low <= high )
{
mid = ( low + high )/2;
if( *( a + mid ) == target )
return mid;
else if( *( a + mid ) < target )
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main( int argc, char *argv[] )
{
type_print_num_func func;
int ( *func2 )( int num );
print_num( 1 );
func = print_num;
func( 2 );
func2 = print_num;
func2( 3 );
system( "pause" );
return 0;
}
更多信息请查看IT技术专栏