2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by javaAddict
0

If your array is an array of Strings the you can use the compareTo method:
http://java.sun.com/javase/6/docs/api/java/lang/String.html#compareTo(java.lang.String)

String s1 = "a";
String s2 = "b";
int i = s1.compareTo(s2);

This method's return values represents which String is greater or lower than the other. So you can use that method for the comparison.

As for the binary search, write a loop that does the following:

>Check if the value to search is in the middle of the array.
>if it's not, check whether it is in the first half or the second half of the array
>After you have found at which half it is, repeat the search, but this time you will not check the entire array but only the half that you found where the value to search is.

remember that the array is already sorted, so the "lower" strings will be at the beginning. That is how you will find which half to choose.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.