Q:

Arrays.binarysearch(double[]array, key) returning -1

I have written a small piece of code to determine the index of the next smaller element in an array. The snippet of code is below:
public static int FindNextSmallest(double[] arr1, int current_small_index) {
    double current_smallest_element = arr1[current_small_index];       

    double[] new_arr1 = arr1.clone();

    Arrays.sort(new_arr1);

    int v = Arrays.binarySearch(new_arr1, current_smallest_element);

    double next_small = new_arr1[v + 1];
    int next_small_index = Arrays.binarySearch(arr1, next_small);
    return next_small_index;
}

The array contains the values: {0.6666,0.6666,0.6666,-0.9333,-0.9333}.
At the beginning, the current_small_index is 3 with current_small_element=-0.9333, Next it returns the next smallest element, which in this case is -0.9333 in the index position 4. But it returns -1.
Please help.

A:

In this statement
int next_small_index = Arrays.binarySearch(arr1, next_small);

You are attempting to apply binarySearch to your original unsorted array.
The binary search method only works on sorted arrays.

