Example 1: binary search java
// Java implementation of iterative Binary Search
class BinarySearch {
// Returns index of x if it is present in arr[],
// else return -1
int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// if we reach here, then element was
// not present
return -1;
}
// Driver method to test above
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at "
+ "index " + result);
}
}
Example 2: binary search java
binary search program in java.
public class BinarySearchExample
{
public static void binarySearch(int[] arrNumbers, int start, int end, int keyElement)
{
int middle = (start + end) / 2;
while(start <= end)
{
if(arrNumbers[middle] < keyElement)
{
start = middle + 1;
}
else if(arrNumbers[middle] == keyElement)
{
System.out.println("Element found at index: " + middle);
break;
}
else
{
end = middle - 1;
}
middle = (start + end) / 2;
}
if(start > end)
{
System.out.println("Element not found!");
}
}
public static void main(String[] args)
{
int[] arrNumbers = {14,15,16,17,18};
int keyElement = 16;
int end = arrNumbers.length - 1;
binarySearch(arrNumbers, 0, end, keyElement);
}
}
Example 3: binary search java
public int runBinarySearchRecursively(
int[] sortedArray, int key, int low, int high) {
int middle = (low + high) / 2;
if (high < low) {
return -1;
}
if (key == sortedArray[middle]) {
return middle;
} else if (key < sortedArray[middle]) {
return runBinarySearchRecursively(
sortedArray, key, low, middle - 1);
} else {
return runBinarySearchRecursively(
sortedArray, key, middle + 1, high);
}
}
Example 4: binary search in java
import java.util.Scanner;
// Binary Search in Java
class Main {
int binarySearch(int array[], int element, int low, int high) {
// Repeat until the pointers low and high meet each other
while (low <= high) {
// get index of mid element
int mid = low + (high - low) / 2;
// if element to be searched is the mid element
if (array[mid] == element)
return mid;
// if element is less than mid element
// search only the left side of mid
if (array[mid] < element)
low = mid + 1;
// if element is greater than mid element
// search only the right side of mid
else
high = mid - 1;
}
return -1;
}
public static void main(String args[]) {
// create an object of Main class
Main obj = new Main();
// create a sorted array
int[] array = { 3, 4, 5, 6, 7, 8, 9 };
int n = array.length;
// get input from user for element to be searched
Scanner input = new Scanner(System.in);
System.out.println("Enter element to be searched:");
// element to be searched
int element = input.nextInt();
input.close();
// call the binary search method
// pass arguments: array, element, index of first and last element
int result = obj.binarySearch(array, element, 0, n - 1);
if (result == -1)
System.out.println("Not found");
else
System.out.println("Element found at index " + result);
}
}
Example 5: binary search implementation using java collections
// Returns index of key in sorted list sorted in
// ascending order
public static int binarySearch(List slist, T key)
// Returns index of key in sorted list sorted in
// order defined by Comparator c.
public static int binarySearch(List slist, T key, Comparator c)
If key is not present, the it returns "(-(insertion point) - 1)".
The insertion point is defined as the point at which the key
would be inserted into the list.