Solved
binary search
Posted on 2011-09-30
i wrote my binary search program as below...
char[] c1 = new char[]{'d','a','f','k','e'};
Arrays.sort(c1);
x = 'f';
binarySearch(c1,x);
public static int binarySearch(char [] c1, char x)
{
int low = 0;
int high = c1.length - 1;
int mid;
while( low <= high )
{
mid = ( low + high ) / 2;
if( c1[ mid ] == x )
low = mid + 1;
else if( c1[ mid ] == x )
high = mid - 1;
else
return mid;
}
return 1000;
}
I am not getting correct results with the above code
if x = 'f'
return value is 2
even if x = 'q'
return value is 2
what is the mistake