![]() |
#2
HJin2007-07-06 06:03
回复:(璇璇贺贺)如何编译 “折半法查找”
/*---------------------------------------------------------------------------
*/ #include <stdio.h> #define DIM(x) sizeof((x))/sizeof((x)[0]) /** Binary search. Pre-condition: the array a is sorted, either Post-condition: return the index of the search key. if(n<1) if(a[0]<=a[n-1]) /// the array is sorted ascending if(a[mid]<key) if(a[mid]<key) return -1;
int i=binary_search(a, DIM(a), 0); printf("i = %d\n",i); return 0; |
用折半法查找
题目是这样的:
一个整形有序数组,假设是{0,1,2,3,4,5,6,7,8,9}
输入一个数字,要求先与中间的数比较,比中间的大,
就只与中间后面的数相比较,否则,与前面的数比较。
哪位大虾帮帮忙,小弟先谢过了~~~