Binary Search
import java.io.*;
public class BinSearch
{
public static boolean binarySearch(String a[], String value)
{
int mid = 0;
int left = 0;
int right = a.length-1;
while (left <= right)
{
mid = (int) Math.floor((left+right)/2);
if (a[mid].equals(value))
return true;
if (value.compareTo(a[mid])>0)
right = mid-1;
else left = mid+1;
}
return false;
}
public static void bubbleSort(String a[])
{
for (int i = 0; i <= a.length-1; i++)
{
for (int j = i+1; j<a.length; j++)
{
if (a[i].compareTo(a[j])>0)
{
String t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
public static void main (String args[]) throws IOException
{
BufferedReader Br = new BufferedReader(new InputStreamReader(System.in));
String array[] = new String[10];
for (int i=0; i < 10; i++)
{
array[i] = Br.readLine();
}
bubbleSort(array);
System.out.println(binarySearch(array,"tony"));
}
}
When I input the 10 Strings, one of them being "tony", it is usually returning false, only a few times it returns true...
Can someone help me with this please? Thanks.

