Binary search does not work with doubles
binary, java, search
Solution
You should change the conditions:
`if (arr[mid] > x)` should be `if (arr[mid] < x)`
`else if (arr[mid] < x)` should be `else if (arr[mid] > x)`
Also note that in order to make this work, the array must be sorted (That's the whole point of binary search), you can use `Arrays#sort`:
Arrays.sort(a);
I recommend you rename your class so it begins with an upper case (Following Java Naming Conventions).
Problem
This program works very well with integers, but not doubles. There are no errors, but the program returns -1. Sorry if this is a stupid question, but I am new to programming. ``` public class binarySearchProject { public static int binarySearch(double[] arr, double x, int high, int low) { int mid=(high+low)/2; if(high==low || low==mid || high==mid) { return -1; } if(arr[mid]>x) { return binarySearch(arr, x, high, mid); } else if(arr[mid]<x) { return binarySearch(arr, x, mid, low); } else if(arr[mid]==x) { return mid; } return -1; } public static void main(String args[]) { double i = 45.3; double[] a = {-3, 10, 5, 24, 45.3, 10.5}; int size = a.length; System.out.println(binarySearch(a, i, size, 0)); } } ```