Binary Search in Java

For the binary search program in Java if the array values are not defined already it will ask the user first to enter the size of the array. Then the user will enter array elements one by one. After adding all the elements to array ask the user to enter the element to search in an array by using binary search.

In binary search all the array elements must be sorted already, otherwise we will need to sort them first. If the duplicate numbers are entered by the user then there is no guarantee which element will be found in the binary search.

Th program first checks whether the element is present at middle if not then the element will be compared with middle value if results greater then it is present in left sub-array otherwise present in right sub-array.

Program

Following are the example programs for binary search using different functions in Java.

Implementing recursive binary search:

By using for loop and taking middle of the array we can perform a binary search on specified array in Java.

```// Java program of recursive Binary Search
public class BinarySearch
{
int binarySearch(int elements[], int first, int last, int x)
{
if (last>=first)
{
int middle = first + (last - first)/2;

// first check if element is present at middle
if (elements[middle] == x)
return middle;

// now check if element is smaller than middle if true then it will
// only present in left subarray
if (elements[middle] > x)
return binarySearch(elements, first, middle-1, x);

// else present at right subarray
return binarySearch(elements, middle+1, last, x);
}

return -1;
}

public static void main(String args[])
{
BinarySearch binarySearch = new BinarySearch();
int elements[] = {10,25,35,40,50};
int length = elements.length;
int x = 35;
int result = binarySearch.binarySearch(elements,0,length-1,x);
if (result == -1){
System.out.println("Element not present");
}
else{
System.out.println("Element found at index " + result);
}
}
}

```

The output should be:

Element found at index 2

2. Using Collections.binarySearch() function:

By using Collections.binarySearch() function we can perform a binary search on object collections like LinkedList or ArrayList in Java.

```// Java program to show working of Collections.binarySearch()

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;

public class CollectionsBinarySearch
{
public static void main(String[] args)
{
List arrayList = new ArrayList();

// find 45 in arraylist which is present at index 2
int element = 45;
int index = Collections.binarySearch(arrayList, element);
if (index >= 0){
System.out.println(element + " found at index "+ index);
}
else{
}

// find 40 in arraylist which is present at index 4
element = 40;
index = Collections.binarySearch(arrayList, element);
if (index >= 0){
System.out.println(element + " found at index "+ index);
}
else{
}

// find 60 in arraylist which is present at index 4
element = 60;
index = Collections.binarySearch(arrayList, element);
if (index >= 0){
System.out.println(element + " found at index "+ index);
}
else{
}
}
}

```

The output should be:

45 found at index = 2
60 found at index = 4

3. Implementing binary search:

By using Arrays.binarySearch() function we can perform a binary search on Arrays.

```// Java program for binarySearch().
import java.util.Arrays;

public class ArrayBinarySearch {

public static void main(String[] args)
{
int items[] = { 15, 30, 40, 55, 75 };
Arrays.sort(items);

// searching vaue 20 which is not present in array
int element = 20;
int result = Arrays.binarySearch(items, element);
if (result >= 0){
System.out.println(element + " found at index "+ result);
}
else{
}

// searching vaue 55 which is present at position 3
element = 55;
result = Arrays.binarySearch(items, element);
if (result >= 0){
System.out.println(element + " found at index "+ result);
}
else{