fork(8) download
  1. int binsearch(int x, int v[], int n)
  2. {
  3. int low, high, mid;
  4.  
  5. low = 0;
  6. high = n - 1;
  7. while(low <= high){
  8. mid = (high+low)/2;
  9. if(x < v[mid]){
  10. high = mid - 1;
  11. }else if(x > v[mid]){
  12. low = mid + 1;
  13. }else{
  14. return mid;
  15. }
  16. printf("mid is %d\n",mid);
  17. }
  18.  
  19. return -1;
  20. }
  21.  
  22. int main(void)
  23. {
  24. int v[] = {2,3,4,7,8,23,54,65,76};
  25. int ret = binsearch(7, v, sizeof(v)/sizeof(int));
  26.  
  27. printf("%d,ret is %d\n", sizeof(v),ret);
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 1676KB
stdin
Standard input is empty
stdout
mid is 4
mid is 1
mid is 2
36,ret is 3