Example 1: binary search program c++
#include <iostream>
using namespace std;
int binarySearch(int array[], int size, int value)
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (array[middle] == value)
{
found = true;
position = middle;
}
else if (array[middle] > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
int main ()
{
const int size = 5;
int array[size] = {1, 2, 3, 4, 5};
int value;
int result;
cout << "What value would you like to search for? ";
cin >> value;
result = binarySearch(array, size, value);
if (result == -1)
cout << "Not found\n";
else
cout << "Your value is in the array.\n";
return 0;
}
Example 2: binary search java
class BinarySearch {
int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
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 3: 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 4: 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 5: binary search in java
import java.util.Scanner;
class Main {
int binarySearch(int array[], int element, int low, int high) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == element)
return mid;
if (array[mid] < element)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
public static void main(String args[]) {
Main obj = new Main();
int[] array = { 3, 4, 5, 6, 7, 8, 9 };
int n = array.length;
Scanner input = new Scanner(System.in);
System.out.println("Enter element to be searched:");
int element = input.nextInt();
input.close();
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 6: binary search
import java.util.Scanner;
public class Binarysearch {
public static void main(String[] args) {
int[] x= {1,2,3,4,5,6,7,8,9,10,16,18,20,21};
Scanner scan=new Scanner(System.in);
System.out.println("enter the key:");
int key=scan.nextInt();
int flag=0;
int low=0;
int high=x.length-1;
int mid=0;
while(low<=high)
{
mid=(low+high)/2;
if(key<x[mid])
{
high=mid-1;
}
else if(key>x[mid])
{
low=mid+1;
}
else if(key==x[mid])
{
flag++;
System.out.println("found at index:"+mid);
break;
}
}
if(flag==0)
{
System.out.println("Not found");
}
}
}