Link to home
Start Free TrialLog in
Avatar of mattkr
mattkr

asked on

Search an array

What is the quickest way to seatch an array that is already sorted?  ANd how do I do it?
Avatar of ozo
ozo
Flag of United States of America image

int min=0;
int max=(sizeof(array)/sizeof(array[0]))-1;
while( min < max ){
       int x = (max+min)/2;
       if( array[x] < target ){
              min = x+1;
       }else if( array[x] > target ){
              max = x-1;
       }else{
              printf("array[%d] = %d\n",x,array[x]);
            max=min=x;
       }
}


ASKER CERTIFIED SOLUTION
Avatar of rbr
rbr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Uhmm... Why not use bsearch() from the standard run-time C library?