Hi I am trying to write a binary search function to search for a specific character in a character array sorted in ascending order. Whenever I run the code though nothing happens, there are no errors or anything, just nothing happpens until I terminate the program myself. Below is my code.
static int binarySearch(char A[], char key) {
int f= 0;
int l= A.length;
while(f < l) {
int mid= (l-f)/2;
int x= A[mid];
if (x == key) return mid;
if (x < key) f= mid;
else l= mid;
}
return f;
}
From what I understand a binary search works by halving the search field until we find the key or the search field is empty, which is what I though my code would do, but I can't see where I've gone wrong.