I just learned about Arrays and came to know about Linear and Binary Search . Is it possible to perform Binary Search on String Array ? I tryed to achieve but failed
here while running the code output is NOT FOUND ; I just can't get it.
String a[] = {"Dossa" , "Maggie" , "Chai" , "Coffe"};
String key = "Coffe";
int index = hwq(a , key);
if(index == -1 ){
System.out.print("NOT FOUND ");
}else{
System.out.print(key + " is at index = " + index);
}
}
public static int hwq(String a[] , String key){
int start = 0 , end = a.length - 1;
while(start <= end){
int mid = ( start + end)/2 ;
if(a[mid] == key ){
return mid ;
}
start ++;
end --;
}
return -1;
}
It would be helpful to know my mistake and also a new or different apporoach to this problem.