1#include <iostream>
2using namespace std;
3
4// This program performs a binary search through an array, must be sorted to work
5int binarySearch(int array[], int size, int value)
6{
7 int first = 0, // First array element
8 last = size - 1, // Last array element
9 middle, // Mid point of search
10 position = -1; // Position of search value
11 bool found = false; // Flag
12 while (!found && first <= last)
13 {
14 middle = (first + last) / 2; // Calculate mid point
15 if (array[middle] == value) // If value is found at mid
16 {
17 found = true;
18 position = middle;
19 }
20 else if (array[middle] > value) // If value is in lower half
21 last = middle - 1;
22 else
23 first = middle + 1; // If value is in upper half
24 }
25 return position;
26}
27int main ()
28{
29 const int size = 5; // size initialization
30 int array[size] = {1, 2, 3, 4, 5}; // declare array of size 10
31 int value; // declare value to be searched for
32 int result; // declare variable that will be returned after binary search
33
34 cout << "What value would you like to search for? "; // prompt user to enter value
35 cin >> value;
36 result = binarySearch(array, size, value);
37
38 if (result == -1) // if value isn't found display this message
39 cout << "Not found\n";
40 else // If value is found, displays message
41 cout << "Your value is in the array.\n";
42
43 return 0;
44}
1// Java implementation of iterative Binary Search
2class BinarySearch {
3 // Returns index of x if it is present in arr[],
4 // else return -1
5 int binarySearch(int arr[], int x)
6 {
7 int l = 0, r = arr.length - 1;
8 while (l <= r) {
9 int m = l + (r - l) / 2;
10
11 // Check if x is present at mid
12 if (arr[m] == x)
13 return m;
14
15 // If x greater, ignore left half
16 if (arr[m] < x)
17 l = m + 1;
18
19 // If x is smaller, ignore right half
20 else
21 r = m - 1;
22 }
23
24 // if we reach here, then element was
25 // not present
26 return -1;
27 }
28
29 // Driver method to test above
30 public static void main(String args[])
31 {
32 BinarySearch ob = new BinarySearch();
33 int arr[] = { 2, 3, 4, 10, 40 };
34 int n = arr.length;
35 int x = 10;
36 int result = ob.binarySearch(arr, x);
37 if (result == -1)
38 System.out.println("Element not present");
39 else
40 System.out.println("Element found at "
41 + "index " + result);
42 }
43}
1#include<iostream>
2using namespace std;
3int binarySearch(int arr[], int p, int r, int num) {
4 if (p <= r) {
5 int mid = (p + r)/2;
6 if (arr[mid] == num)
7 return mid ;
8 if (arr[mid] > num)
9 return binarySearch(arr, p, mid-1, num);
10 if (arr[mid] > num)
11 return binarySearch(arr, mid+1, r, num);
12 }
13 return -1;
14}
15int main(void) {
16 int arr[] = {1, 3, 7, 15, 18, 20, 25, 33, 36, 40};
17 int n = sizeof(arr)/ sizeof(arr[0]);
18 int num = 33;
19 int index = binarySearch (arr, 0, n-1, num);
20 if(index == -1)
21 cout<< num <<" is not present in the array";
22 else
23 cout<< num <<" is present at index "<< index <<" in the array";
24 return 0;
25}
1#binary search python
2def binaryy(ar, ele):
3 low = 0
4 high = len(ar)-1
5 if ele not in ar:
6 return "Not Found"
7 while low <= high:
8 mid = (low + high) // 2
9 if ar[mid] < ele:
10 low = mid + 1
11 elif ar[mid] > ele:
12 high = mid - 1
13 else:
14 return mid
15
16
17ar = [10, 20, 30, 40, 50]
18ele = 55
19print(binaryy(ar, ele))
1#include <bits/stdc++.h>
2using namespace std;
3
4int main(){
5 int n;
6 cin>>n;
7 vector<int>v(n);
8 for(int i = 0; i<n; i++){
9 cin>>v[i];
10 }
11 int to_find;
12 cin>>to_find;
13 int lo = 0 , hi = n-1 , mid ;
14 while(hi - lo > 1){
15 int mid = (hi + lo)/2;
16 if(v[mid] < to_find){
17 lo = mid + 1;
18 }else{
19 hi = mid;
20 }
21 }
22 if(v[lo] == to_find){
23 cout<<lo<<endl;
24 }else if(v[hi] == to_find){
25 cout<<hi<<endl;
26 }else{
27 cout<<"Not Found";
28 }
29
30 return 0;
31}
1#include <bits/stdc++.h>
2
3using namespace std;
4
5int binarySearch(int arr[], int l, int h, int key){
6 if(l<=h){
7 int mid = l + (h-l)/2;
8
9 if(arr[mid] == key){
10 return mid;
11 }
12
13 else if(arr[mid] > key){
14 return binarySearch(arr, l, mid-1, key);
15 }
16
17 else if(arr[mid] < key){
18 return binarySearch(arr,mid+1, h, key);
19 }
20 }
21
22 return -1;
23}
24
25int main(){
26 int arr[] = {1,2,3,4,5,6,7,8,9,10};
27 int n = sizeof(arr)/sizeof(arr[0]);
28 int key = 7;
29
30 int result = binarySearch(arr,0,n-1,key);
31
32 (result==-1)
33 ? cout << "Element is not found in the array" << endl
34 : cout << "Element is found at index " << result;
35
36 return 0;
37
38}