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// BY shivam kumar KIIT
2#include<bits/stdc++.h>
3usind namespace std;
4int main()
5{
6 int arr[]={10,2,34,2,5,4,1};
7 sort(arr,arr+7);//sort array in ascending order before using binary search
8 binary_search(arr,arr+7,10);//return 1 as element is found
9 binary_search(arr,arr+7,3);//return 0 as element is not found
10 return 0;
11}
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#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//requires header <algorithm> for std::binary_search
2#include <algorithm>
3#include <vector>
4
5bool binarySearchVector(const std::vector<int>& vector,
6 int target) {
7 //this line does all binary searching
8 return std::binary_search(vector.cbegin(), vector.cend(), target);
9}
10
11#include <iostream>
12
13int main()
14{
15 std::vector<int> haystack {1, 3, 4, 5, 9};
16 std::vector<int> needles {1, 2, 3};
17
18 for (auto needle : needles) {
19 std::cout << "Searching for " << needle << std::endl;
20 if (binarySearchVector(haystack, needle)) {
21 std::cout << "Found " << needle << std::endl;
22 } else {
23 std::cout << "no dice!" << std::endl;
24 }
25 }
26}
1int result = -1;
2 int low = 0;
3 int high = N-1; // N - # of elements
4 while (low <= high)
5 { mid = (low + high) / 2;
6 if ( item == vector[mid])
7 { result = mid;
8 break;
9 }
10 else if (item > vector[mid] )
11 { low = mid + 1; }
12 else { high = mid - 1; }
13 }
14