1

I am new to programming. I have six month that i have started learning programming and i trying to test my self with some algorithm. I am trying to implement the binary search algorithm. In theory I have grasped the concept but in implementation i am having some trouble.

Below is the algorithm implementation:

    public static boolean binarySearchNumber(int[] numbers, int number) {
        Arrays.sort(numbers);
        System.out.println(Arrays.toString(numbers));
        int lowIndex = 0;
        int highIndex = numbers.length;
        while(lowIndex!=highIndex) {
            int midIndex = (lowIndex+highIndex)/2;
            if(numbers[midIndex]==number) {
                return true;
            } else if(numbers[midIndex]>number) {
                lowIndex = midIndex+1;
            } else if(numbers[midIndex]<number) {
                highIndex = midIndex-1;
            }
        }
        return false;
    }

The unit test

    @Test
    public void testBinarySearchNumber() {
        // setup
        int[] numbers = new int[] { 1, 3, 55, 8, 22, 9, 11, 0 };
        // execute
        boolean found = ArrayUtil.binarySearchNumber(numbers, 8);
        System.out.println(found);

    }

Thank you in advance.

1
  • I am trying to implement algorithm by myself to help create a programming logic. Commented Feb 5, 2020 at 9:54

2 Answers 2

1

beyond the error reported in the other answers, I found another one:

public static boolean binarySearchNumber(int[] numbers, int number) {
        Arrays.sort(numbers);
        System.out.println(Arrays.toString(numbers));
        int lowIndex = 0;
        int highIndex = numbers.length;
        while (lowIndex != highIndex) {
            int midIndex = (lowIndex + highIndex) / 2;
            if (numbers[midIndex] == number) {
                return true;
            } else if (numbers[midIndex] < number) {
                lowIndex = midIndex + 1;
            } else if (numbers[midIndex] > number) {
                highIndex = midIndex ; // here
            }
        }
        return false;
    }

midIndex is in array, you don't need to subtract 1, otherwise you will always have one element less.

EDIT

Another manner to achieve the desired results is change the if condition as suggested by @Eran's answer.

Sign up to request clarification or add additional context in comments.

2 Comments

Your highIndex = midIndex ; // here change is not required if you change the loop's condition and the initial value of highIndex, as in my answer.
@Eran I noticed the change in IF conditions only after replying. There was also another answer that only highlighted the "else if" problem and I assumed, at first glance, that your answer was similar. Anyway edited my answer, upvoted yours: the JDK implementation is interesting.
1

You have several issues:

public static boolean binarySearchNumber(int[] numbers, int number) {
    Arrays.sort(numbers);
    System.out.println(Arrays.toString(numbers));
    int lowIndex = 0;
    int highIndex = numbers.length - 1; // the last index is numbers.length - 1
    while (lowIndex <= highIndex) { // you have to allow lowIndex to be equal to highIndex
                                    // or you'll fail to locate the lowest and highest numbers
        int midIndex = (lowIndex+highIndex)/2;
        if(numbers[midIndex]==number) {
            return true;
        } else if(numbers[midIndex] < number) { // you used the wrong comparison operator here
            lowIndex = midIndex+1;
        } else if(numbers[midIndex] > number) { // you used the wrong comparison operator here
            highIndex = midIndex-1;
        }
    }
    return false;
}

Out of curiosity, I looked at the JDK implementation of binary search in Arrays class, and it is very similar to my solution (apart from the JDK's method returning the index instead of a boolean).

private static int binarySearch0(long[] a, int fromIndex, int toIndex,
                                 long key) {
    int low = fromIndex;
    int high = toIndex - 1; // that's equivalent to a.length - 1 if we search the whole array

    while (low <= high) {
        int mid = (low + high) >>> 1; // that's equivalent to (low + high) / 2
        long midVal = a[mid];

        if (midVal < key)
            low = mid + 1;
        else if (midVal > key)
            high = mid - 1;
        else
            return mid; // key found
    }
    return -(low + 1);  // key not found.
}

2 Comments

This also solves the error. I would vote this up. But i don't have priveleges to do so. Thanks a lot for you effort.
Thank you Eran. I voted your question up. This is also a correct solution of binary search algorithm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.