int binsearch(int x, int v[], int n)
{
int low, high, mid;
low = 0;
high = n - 1;
while(low <= high){
mid = (high+low)/2;
if(x < v[mid]){
high = mid - 1;
}else if(x > v[mid]){
low = mid + 1;
}else{
return mid;
}
}
return -1;
}
int main(void)
{
int v[] = {2,3,4,7,8,23,54,65,76};
int ret = binsearch(7, v, sizeof(v)/sizeof(int));
printf("%d,ret is %d\n", sizeof(v
),ret
); return 0;
}
aW50IGJpbnNlYXJjaChpbnQgeCwgaW50IHZbXSwgaW50IG4pCnsKICAgIGludCBsb3csIGhpZ2gsIG1pZDsKCiAgICBsb3cgPSAwOwogICAgaGlnaCA9IG4gLSAxOwogICAgd2hpbGUobG93IDw9IGhpZ2gpewogICAgICAgIG1pZCA9IChoaWdoK2xvdykvMjsKICAgICAgICBpZih4IDwgdlttaWRdKXsKICAgICAgICAgICAgaGlnaCA9IG1pZCAtIDE7CiAgICAgICAgfWVsc2UgaWYoeCA+IHZbbWlkXSl7CiAgICAgICAgICAgIGxvdyA9IG1pZCArIDE7CiAgICAgICAgfWVsc2V7CiAgICAgICAgICAgIHJldHVybiBtaWQ7CiAgICAgICAgfQogICAgICAgIHByaW50ZigibWlkIGlzICVkXG4iLG1pZCk7CiAgICB9CgogICAgcmV0dXJuIC0xOwp9CgppbnQgbWFpbih2b2lkKQp7CiAgICBpbnQgdltdID0gezIsMyw0LDcsOCwyMyw1NCw2NSw3Nn07CiAgICBpbnQgcmV0ID0gYmluc2VhcmNoKDcsIHYsIHNpemVvZih2KS9zaXplb2YoaW50KSk7CgogICAgcHJpbnRmKCIlZCxyZXQgaXMgJWRcbiIsIHNpemVvZih2KSxyZXQpOwogICAgcmV0dXJuIDA7Cn0=