Example 1: binary search program c++
#include <iostream>
using namespace std;
// This program performs a binary search through an array, must be sorted to work
int binarySearch(int array[], int size, int value)
{
int first = 0, // First array element
last = size - 1, // Last array element
middle, // Mid point of search
position = -1; // Position of search value
bool found = false; // Flag
while (!found && first <= last)
{
middle = (first + last) / 2; // Calculate mid point
if (array[middle] == value) // If value is found at mid
{
found = true;
position = middle;
}
else if (array[middle] > value) // If value is in lower half
last = middle - 1;
else
first = middle + 1; // If value is in upper half
}
return position;
}
int main ()
{
const int size = 5; // size initialization
int array[size] = {1, 2, 3, 4, 5}; // declare array of size 10
int value; // declare value to be searched for
int result; // declare variable that will be returned after binary search
cout << "What value would you like to search for? "; // prompt user to enter value
cin >> value;
result = binarySearch(array, size, value);
if (result == -1) // if value isn't found display this message
cout << "Not found\n";
else // If value is found, displays message
cout << "Your value is in the array.\n";
return 0;
}
Example 2: binary search in c++
#include<iostream>
using namespace std;
int binarySearch(int arr[], int p, int r, int num) {
if (p <= r) {
int mid = (p + r)/2;
if (arr[mid] == num)
return mid ;
if (arr[mid] > num)
return binarySearch(arr, p, mid-1, num);
if (arr[mid] < num)
return binarySearch(arr, mid+1, r, num);
}
return -1;
}
int main(void) {
int arr[] = {1, 3, 7, 15, 18, 20, 25, 33, 36, 40};
int n = sizeof(arr)/ sizeof(arr[0]);
int num = 33;
int index = binarySearch (arr, 0, n-1, num);
if(index == -1)
cout<< num <<" is not present in the array";
else
cout<< num <<" is present at index "<< index <<" in the array";
return 0;
}
Example 3: binary search algorithm
#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[], int l, int h, int key){
if(l<=h){
int mid = l + (h-l)/2;
if(arr[mid] == key){
return mid;
}
else if(arr[mid] > key){
return binarySearch(arr, l, mid-1, key);
}
else if(arr[mid] < key){
return binarySearch(arr,mid+1, h, key);
}
}
return -1;
}
int main(){
int arr[] = {1,2,3,4,5,6,7,8,9,10};
int n = sizeof(arr)/sizeof(arr[0]);
int key = 7;
int result = binarySearch(arr,0,n-1,key);
(result==-1)
? cout << "Element is not found in the array" << endl
: cout << "Element is found at index " << result;
return 0;
}
Example 4: binary search
//Binary search can apply to sorted data only.
//Time complexity of binary search is O(log n ).
//It always divide the whole data in parts and compare a search key to middle element only.
import java.util.*;
public class BinarySearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int[] a = {10,20,50,30,40};
int key=sc.nextInt();
Arrays.sort(a); // An method in java.util.Arrays package to sort an array element.
int first=0,end=a.length-1,mid=0,flag=0;
while(first<=end)
{
mid=(first+end)/2;
if(key<a[mid]) // Move to left part if key is smaller than middle element.
{
end = mid-1;
}
else if(key>a[mid]) // Move to right part if key is greater than middle element.
{
first = mid+1;
}
else
{
flag=1;
break;
}
}
if(flag==1)
{
System.out.println("Success! found");
}
else
{
System.out.println("Error! This key (" + key + ") does not exist in the array");
}
}
}
Example 5: binary search
function binarySearchRicorsivo(array A, int p, int r, int v)
if p > r
return -1
if v < A[p] or v > A[r]
return -1
q= (p+r)/2
if A[q] == v
return q
else if A[q] > v
return binarySearchRicorsivo(A,p,q-1,v)
else