• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Java Binary Search Help

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

stereosteve99

Member
Joined
Aug 12, 2002
Location
Howell, Michigan
I can get it to compile, but it never gets to the part where it finds it or doesn't.. Goes through the while loop 4 times, no matter what the entry is.


public class BinarySearch

{

public static void main(String[] args) throws Exception

{

char[] orderedList = {'A','B','E','G','L','P','V','W','Y','Z'};

System.out.println("Enter a capital letter to search for: ");

int x = System.in.read();






int count = 0;
int low = 0;

int high = orderedList.length - 1;

while (low <= high) {

count++;
int mid = (low + high) / 2;



if (x < mid)

high = mid - 1;

else if (x > mid)

low = mid + 1;

else

System.out.println("It has been found!");

}

if (high > low)

System.out.println("Number wasn't found");

System.out.println(count);
}

}
 
I will look again to make sure your algorithm is correct, but the biggest problem you have now is that you are mixing chars and ints.

You have a char array of capital letters, and then you read input with this line:
int x = System.in.read();
which java casts as an integer. The integer equivalent of a char is the character's ascii code (ie the ascii representation for 'A' is 65 which is well out of your search range).

You are also making a incorrect comparisons:
if (x < mid)
should be
if (x < orderedlist[mid])
Otherwise you are comparing the char you are searching for to the index of a char in the array.

Let me know if that was confusing, and I'll be glad to assist you further.
 
Here's the update, now I have no idea why it doesn't work. If I enter L(the middle number) it works, otherwise it doesn't


public class BinarySearch


{


public static void main(String[] args) throws Exception


{


char[] orderedList = {'A','B','E','G','L','P','V','W','Y','Z'};


System.out.println("Enter a capital letter to search for: ");


int x = System.in.read();









int count = 0;

int low = 0;


int high = orderedList.length - 1;


while (low < high) {


count++;

int mid = (low + high) / 2;





if (x < orderedList[mid])


high = mid - 1;


else if (x > orderedList[mid])


low = mid + 1;


else


System.out.println("It has been found!");


high = low;
}


if (high > low)


System.out.println("Number wasn't found");


System.out.println(count);

}


}
 
Last edited:
Thanks for all your help on that one. I actually wanted to keep the x an integer because I was using the unicode, like you said earlier. The only thing I had to fix, which ****es me off so much, because it was so easy was putting curly brackets around all my if statements and I changed a couple small things. Now it works. But I always thought that if you only have one line after an if you didn't need the brackets.. God I hate programming..

Thanks again, that one had me stumpted (Which isn't saying much for me.)
 
Back