I have this code, but I don't know what's wrong, I've tested that the compiler is sorting correctly, but strangely the main() method returns false even if the String in the binarySearch() parameter matches one of the Strings input by me.... :confused:
Code:
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();
}