Binary Search Java example. Easy solution. • IT Blog
IT Blog
Boris IT Expert

Binary Search Java example. Easy solution.

Boris~November 12, 2020 /Search Algorithms

In Java, binary search is the preferable solution than the linear search. It is a better solution from a complexity point of view. Because in comparison with the linear search, you need much fewer iterations to find the needed element.

The main idea of the binary search.

The main idea here is to make a pointer that will show the middle of the array list. Then we need to check if the searched element value less or more than the middle pointer. If it is less, we are moving the pointer and the right end to the array list’s left side. In case the value is higher than the pointer, we are moving the middle pointer and the left end to the right side. As a result, we divide our array list in half. Then the half result we divide in half again. We should repeat that process until we find the needed element. One important moment is that we need to sort the list first. Let’s try to discuss every step in detail.

Example explanation? The best solution in Java.

Step-by-step binary search solution. Code example with pictures and diagram explanations below:

Part one. We need to prepare all pointers and start looping:

//Start point of search
        int leftPointer = 0;
//End point of search
        int rightPointer = array.length - 1;
//Value we are looking for
        int searchedItem = 12;
//Divide and conquer principe looping
        while (leftPointer <= rightPointer)
//Setting the middle of the array
            int middlePointer = (leftPointer + rightPointer) / 2;

Part two. Implement three conditions that help us find the element:

 //If searched item less that middle
            if (searchedItem < array[middlePointer]) {
//Moving search part to left of the middle by setting end point to mid - 1
                rightPointer = middlePointer - 1;
            } else {
//If searched item more than middle
//Moving search part to right of the middle by setting start point to mid + 1
                leftPointer = middlePointer + 1;
            }
//If middle value the same as search we have found the item
            if (array[middlePointer] == searchedItem) {
                Log.d("arrayList", "---> " + middlePointer);
                return;
            }

Animation:

Full Schema:

binary search java example
binary search java example

Full code solution:

private void binarySearch() {
        //Creating array of int
        int[] array = new int[]{2,5,6,8,9,12};

        //Start point of search
        int leftPointer = 0;
        //End point of search
        int rightPointer = array.length - 1;

        //Value we are looking for
        int searchedItem = 12;

        //Result index we are looking for
        int result = -1;
        //Divide and conquer principe looping
        while (leftPointer <= rightPointer) {
 //Setting the middle of the array
            int middlePointer = (leftPointer + rightPointer) / 2;

//If middle value the same as search we have found the item
            if (array[middlePointer] == searchedItem) {
                Log.d("arrayList", "---> " + middlePointer);
                return;
            }

            //If searched item less that middle
            if (searchedItem < array[middlePointer]) {
//Moving search part to left of the middle by setting end point to mid - 1
                rightPointer = middlePointer - 1;
            } else {
                //If searched item more than middle
//Moving search part to right of the middle by setting start point to mid + 1
                leftPointer = middlePointer + 1;
            }

        }

        Log.d("arrayList", "Nothing have been found");
    }

In this article, I explained the principles of the binary search Java.

Full code and more examples in my facebook group.

Useful links:

Leave Any Questions Related To This Article Below!