Shortcut for chapter specific information

Friday, May 13, 2011

Binary Search 4 (C)

Just back record : I found my binary search (BSRCH 3) was flawed.  In the C-version. It is fixed and it looks like,

     1    int binary_search(int a[],int l,int h,int key)
     2    {
     3      while(l<=h){
     4        int m = l + (h-l)/2;
     5        if(key > a[m])
     6          l=m+1;
     7        else if(key==a[m])
     8          return m;
     9        else /* key < a[m] */
    10          h=m-1;
    11      }
    12      return -1;
    13    }

No comments:

Post a Comment