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 in c
#include<stdio.h>
int BinarySearch(int arr[], int search, int mid, int len){
if(mid == -1 || mid == len+1){
printf("\nSearched Element doesn't exist.");
return 1;
}
else if (search > arr[mid]){
mid++;
BinarySearch(arr,search,mid,len);
return 0;
}
else if (search < arr[mid]){
mid--;
BinarySearch(arr,search,mid,len);
return 0;
}
else if(search == arr[mid]) {
printf("\n Searched Element found at Location %d.",mid);
return 1;
}
}
void main(){
int arr[] = {1,2,3,4,5,6,7,8,9};
int len = sizeof(arr) / sizeof(int);
int mid = (int) (len / 2) + 1;
printf("\n Please Enter Number You Want to Search \n > ");
int search;
scanf("%d",&search);
int Result = BinarySearch(arr,search,mid,len);
}
Example 4: 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");
}
}
}
Example 5: binary search
#include <iostream>
using namespace std;
void binarysearch(int arr[],int start,int end,int val)
{
if(start<end)
{
int mid=(start+end)/2;
if(arr[mid]==val)
{
cout<<"value found:"<<endl;
}
else if(arr[mid]<val)
{
binarysearch(arr,mid+1,end,val);
}
else if(arr[mid]>val)
{
binarysearch(arr,start,mid-1,val);
}
}
else
{
cout<<"not present:"<<endl;
}
}
int main()
{
int n;
cout<<"enter the size of the array:"<<endl;
cin>>n;
int arr[n];
cout<<"enter the elements of the array:"<<endl;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"enter the value you want to search:"<<endl;
int val;
cin>>val;
binarysearch(arr,0,n-1,val);
return 0;
}
Example 6: Implement a binary search of a sorted array of integers Using pseudo-code.
# Here's the pseudocode for binary search, modified for searching in an array. The inputs are the array, which we call array; the number n of elements in array; and target, the number being searched for. The output is the index in array of target:
1.Let min = 0 and max = n-1.
2. Compute guess as the average of max and min, rounded down (so that it is an integer).
3. If array[guess] equals target, then stop. You found it! Return guess.
4. If the guess was too low, that is, array[guess] < target, then set min = guess + 1.
5. Otherwise, the guess was too high. Set max = guess - 1.
6. Go back to step 2.