my_blk_bsrch.c 1.57 KB
Newer Older
priyank's avatar
priyank committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/**
 * File Name : my_blk_bsrch.c.c
 */



/** Fuction : my_blk_bsrch
  * This function returns the ptr to the location of the key in the table, if it is present 
  * else returns a null
  * It takes arguments: key of char type ,base of char type ,nel of int type 
  * width of int type and compar is function with integer return type
  * Return : void 
  */
void *my_blk_bsearch(key,base,nel,width,compar)
	char *base,*key;
	int nel,width;
	int (* compar)();
	{
/* Given the table start address - base,
	 the table size - nel,
         the table width - width,
	 the comparison function - compar,
   this function returns the ptr to the location of the key
   in the table, if it is present else returns a null 
*/
	int low,high,mid,ans,pos;
	low = 0; 
	high = nel-1;
	while (low <= high)  /* executes only when low < = high */
             {
		mid = (low + high)/2;
		ans = (* compar)(key,(char *)(base+mid*width));
		/* printf("The string being tested is %s\n",(char *)(base+mid*width)); */
		if (ans < 0)  /* checks for ans is <0 */
                   high = mid -1;
		else if (ans > 0)   
                    low = mid +1;
		      else 
                        {
                         pos=mid;
              		  ans = (* compar)(key,(char *)(base+pos*width));
              		  while((!ans)&&(pos>0))
	 	   	    {
	                      pos--;
		              ans = (* compar)(key,(char *)(base+pos*width));
                  	     }
	                  if(ans!=0) pos++;
        	          return((void *) (base + pos*width));
                       }
	        }
	return ((void *) '\0');
      }