hi,
can anyone pls find out the problem of my code.
Code:
public class binarysearch {
public static void main ( String[] args ) {
int[] array = {2,2,10,20,55,100};
int target = 2;
int left = 0;
int right = array.length - 1;
int mid = -1;
while ( left <= right ) {
mid = ( left + right ) / 2;
if ( target == array[mid] )
break;
else if ( target < array[mid] )
right = mid - 1;
else
left = mid - 1;
}
if ( mid != -1 )
System.out.println ( target + " found at index " + mid );
else
System.out.println ( target + " not found" );
}
}
Problem :
here it's only showing me the 1st position, but i want it to show me both the position. can anyone pls fix-up the error of my code.
thank you
shantuli