I am working through one of the examples in the Intro to Java textbook 10th edition by Daniel Liang. I ran into an issue in Chapter 7 when performing a binary search on a single dimensional array. My code works fine when I use it on an integer array, but it does not work when I input a double.
update: 4/25/2020 1:20pm Eastern Time USA
I updated the code to reflect the double array and double key in method signature. I have also include a method call. I tried the approximation solution by introducing
static boolean equals(double a, double b){
if (Math.abs(a - b) <= epsilon) return true;
return false;
and then calling it in the binary search method as follows:
while(high>=low){
int mid = (int) ((low+high)/2);
if (key < a[mid])
high = mid - 1;
else if (equals(key, a[mid]))
return mid;
else
low= mid + 1;
I am unsure if the issue is losing precision because even though I am using doubles, they are all whole numbers. Below is the original code from the textbook- method signature changed to reflect double.
class Scratch {
//I created two class array variables here to test the binary search approach.
static double[] myList = {12.0,34.0,5.0,6.0,78.0,89.0,0.0};
static int[] a = {5,1,3,3,4};
//Main method call
public static void main(String[] args) {
System.out.println(binarySearch(myList, 12.0));
}
public static int binarySearch(double[] a, double key){
int low = 0;
int high = a.length-1;
while(high>=low){
int mid = (int) ((low+high)/2);
if (key < a[mid])
high = mid - 1;
else if (key == a[mid])
return mid;
else
low= mid + 1;
}
return -low -1;
}
The computer is printing a negative number signaling that the key was not found.
Thank you!