binary search function in c 2b 2b

Solutions on MaxInterview for binary search function in c 2b 2b by the best coders in the world

showing results for - "binary search function in c 2b 2b"
Sofie
03 Sep 2016
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}
Angeline
20 Apr 2019
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}
Samuel
14 Sep 2019
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}
Jonah
04 Aug 2016
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}
Jeanne
10 Jul 2020
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}
Alessia
01 Jan 2019
1//By Sudhanshu Sharan
2#include<iostream>
3#include<cmath>
4using namespace std;
5// BCT= o(1)  and  WCT=O(logn)   time taken for unsucessful search is always o(logn)
6
7int BinSearch( int arr[],int key,int len)
8{
9	int h,mid,l;
10	l=0;
11	h=len-1;
12	while(l<=h)
13	{
14		mid=((l+h)/2);
15		if(key==arr[mid])
16			return mid;
17		else if(key<arr[mid])
18			h=mid-1;
19		else
20			l=mid+1;
21	}
22	return -1;
23}
24int main()
25{
26	int key,i,len;
27	int arr[] = {1,2,3,6,9,12,15,34,54};
28	len=sizeof(arr)/sizeof(arr[0]);
29	cout<<"enter the key to be searched";
30	cin>>key;
31	int result= BinSearch(arr,key,len);
32	(result == -1)
33		? cout<<"Element is not present in the array"<<endl
34		: cout<<"Element is present at index : "<<result<<endl;	
35	for(i=0;i<len-1;i++)
36		cout<<arr[i]<<" ";
37    return 0;
38}
queries leading to this page
binary search code c 2b 2bhow to use built in binary search function c 2b 2b to find index of valuebinary search in array in cbinary search function c 2b 2bbinary search to find lower boundbinary search in function c 2b 2bbinary search tree c 2b 2b codebinary search c 2b 2b stl descending orderimplement binary search in cc 2b 2b binary search in arrayrecursive binary search c 2b 2bbinary search stl in c 2b 2bbineary search in c 2b 2bbinary search stl cpalgorithm c 2b 2b binary searchbinary search using recursion in cppbinary sbinary search cde in c 2b 2bbinary seach algo in c 2b 2b stlbinary search library in c 2b 2bbinay search stl in c 2b 2bbinary search program in cppprogram of binary search in c 2b 2bbinary sarchbinary search stl for vector of stringsc 2b 2b program to binary searchstl binary search try c 2b 2bbinary search by c 2b 2bcode c 2b 2b exercises binary searchbinary search using c 2b 2b thumbnail pngc 2b 2b program for binary search in an array using sortingcpp binary search algorithmprint each iteration in vector sort binary search in c 2b 2bbinary search inbuilt c 2b 2bimplement binary search tree for strings in c 2b 2brecursive function for binary search in cppcode for binary search in c 2b 2bc 2b 2b binary search in mainhow to code binary search in c 2b 2bhow to write function binary search array in c 2b 2bbinary search in c 2balgorithm binary search c 2b 2bbinary search in c 5cbinary search stl in cppbinary search examplebin search c 2b 2bc 2b 2b binary search stdbinary search implementions c 2b 2bbinary search code for c 2b 3dbinary search c 2b 2bcstlbinary search algorithmin c 2b 2bwhat does binary search return c 2b 2bbinary search function in c 2b 2bbinary search in c 2b 2b stlbinary search c 2b 2b algorithmbinary search using cbinary search c 2b 2b for answerbinary seach stlhow to binary search c 2b 2bdefault binary function in c 2b 2bwrite a program for binary searchcpp binary searchbinary search in c 2b 2b with arraybinay searchhow to do a binary search in c 2b 2bc 2b 2b binary searchbinary seach cppbinary serarch c 2b 2b codebinzry search vectorleftbound and right bound in cpp binary serach binray search in c 2b 2bimplement binary search for array in c 2b 2bbinary search c 2b 2b stlvector binary search c 2b 2bbinary search on vectorbinary search in algorith in c 2b 2bbinary searching c 2b 2bbinary search stl algorithmwhenever i use binary search in c 2b 2b it returns zeroc 2b 2b binary search iterativebinary search function in c 2b 2b in which header filebinary sort c 2b 2bcpp std binary searchbinary search c 2b 2b programc 2b 2b utility binary searchbinary search algorithm in data structure c 2b 2b codec 2b 2b can the binary search algorithm work on anythingwrite a program to search an element in an array using binary search in c 2b 2bbinary search for decresing array in c 2b 2bimplement binary search in c 2b 2bcan binary search search string c 2b 2bbinary search function in c implementationbinary search in c programbinary search c 2b 2biterative binary search in c 2b 2bbinary search in c 2f 2b 2bbinary search c 2b 2b code c 2b 2bbianry search position c 2b 2b stlc 2b 2b binary search tree searchbinary search cppcpp binary search vectorpyhton c code for binary searchstl binary search c 2b 2bhow to implement binary search in cpp stlbinary search in c 2b 2bprint each iteration in vector binary search in c 2b 2bhow to do binary search in c 2b 2b using stlbinary search sort program in c 2b 2bsearch element using binary searchbinary search in stlbinary search simple program in cppprogram for binary search in cbinary search in javasearching in c 2b 2b stlbinary search c 2b 2b indexbinary search recursive c 2b 2b codebinary search for array of element in cppbinary searchc 2b 2bvetor binary searchbinary search function c 2b 2barray binary search algorithm c 2b 2busing iterative binary search in a survey in c 2b 2bwhat is binary searchc 2b 2b binary search codebinary search element in c 2b 2bbinary search vectorbinaert search c 2b 2b stlhow to do binary search in stl n c 2b 2bbinary search in c 2b 2b stlc 2b 2b algorithm binary searchto search element using binary searchbinarysearch cppc 2b 2b binary search functioncpp stl binary search vectorbinary search snippet cppstl array binary searchc 2b 2b binary search stlbinary search array c 2b 2bcpp program for binary searchwrite a program to search a key in a list using binary searchbinary search iterative in cppbinary search pythonnbinary search com 22feedback 22binary search cpp gfgimplementation of binary searchbinary search stl in c 2b 2bbinary dearch in c 2b 2b using stlupperbound search in vector c 2b 2bbinary search in cpp programbinary search c 2b 2b stlreturn index binary search cppdichotomic search c 2b 2bbinary seach c 2b 2bbinary search in c 2b 2b arraysbinary searchingbinary search c 2b 2b iterativebinary search code cppbinary search exampe c 2b 2bbinary searching in c 2b 2btime order of binary search in cppc 2b 2b binary search functuoncpp program to search an element in an array using recursive binary searcghbinary search tree program in c 2b 2bbinary serach cpparray binary search stlbinary and linear search c 2b 2bc 2b 2b algorithm std binary searchbinary search program using divide and conquerbinary search using stlwhat does binary search return in c 2b 2bbinary search for strings c 2b 2bbinary search cpp stlstring binary search in cppbinary searc cbinary search programbinary search in c 2b 2b programhow to use binary search in c 2b 2bbinary search for strings c 2b 2b flawsbinary search inside binary searchbinary search c 2b 2b functionbineary search c 2b 2bbinary search by user input using cppvector binary searchc 2b 2b binary serachbinary searching in array c 2b 2bc 2b 2b function to do binary search in an arraybinary search c 2b 2b stdc binary searchbinary search using divide and conquer c 2b 2bbinary search function in c 2b 2bbinary search stl in arraybinary search vector of strings c 2b 2bbinary search of a number in c 2b 2bbinary search application i cppbinary search in 2b 2bbinary search in array program in c 2b 2bbinary search function c 2b 2b stl return indexbinary search function c 2b 2b array with whileimplementation binary search cbinary searcg vectirc 2b 2b vector bsearchbinary search using c 2b 2bbinary search function stl in c 2b 2bbinary search in array c 2b 2b examplecode binary search examplebinary search sorted array c 2b 2bbinary search in cbinary search in vector of maybeuninitbinary search in c 2b 2b using stlbinary search c programbbinary searach code in c 2b 2biterative binary search in vector 28array 29 return index and print c 2b 2bbinary search c 2b 2bc 2b 2b binary search ufnctionbinary search c 2bbinary search stl function syntaxvector find binary searchlowerbound bin search codebinary serach in c 2b 2bbinary search in cppbinary search code gfg c 2b 2bcoding binary searchbinary search in an array in cc 2b 2b vector binary search examplebinary search in c coderecursive binary search in c 2b 2bbinary search stl 3bc 2b 2b binary search int arraybinary searh in c 2b 2bbinary search cpp referencebinary search in recursion c 2b 2bc 2b 2b binary search algorithmbinary search function in stl c 2b 2bbinary search in cpp stlbinraysearch stlhow to binary search in cppbinary search stlbinary search program in c 2b 2bbinary search implementation c 2b 2bbinary search algorithm c 2b 2bcode c 2b 2b exercices binary searchfind upper index using binary searchbinary search upper bound c 2b 2bbinary search in vector low high c 2b 2bbinary search c 2b 2bbinary search algorithm c 2b 2bbinary search example in javabinary search array cpppbinary search in array stlbinary search in c 2b 2b using functionbinary search codes c 2b 2bc program to find complexity of binary searchcode to find element in the binary search tree c 2b 2bbinarysearch in stl c 2b 2bwhat is binary search in c 2b 2bc 2b 2b vector binary searchbinary search algorithmbinary search in standard library c 2b 2bbinary search array cppcreate binary search tree program in c 2b 2bbinary search in array c 2b 2bbinary search in c 2b 2b vectorbinary search in c binary search algorithm c binary search c 2b 2b syntaxbinary search c 2b 2bstl in vectorwhy binary search in stl does not return the position of found valuec 2b 2b recursive binary searchbinary search of array in cbinary search in c 2b 2b arraybinary search c 2b 2b stl return valueusing binary search stlbinary seaarch program in c 2b 2bprograms on binary searchbinary seach codecpp binary search stlbinary search 28recursive 29bianry searchc 2b 2b binary search vector stlbinary sort in c 2b 2bbinary search cppbinary search to find an element in array binary search cpp builtin binary search stl c 2b 2bsearch function in random binary search tree in c 2b 2bbinary search program using divide and conquer in cbinary search function in c 2b 2b stlbindary search in cbinary search algorithm for cppbinary searchh cppbinarysearch in cbinary search in c 2b 2b geeksforgeeksbinary search lower boundfind by binary search c 2b 2b stlbinary search recursive c 2b 2bhow to make a binary search algorithm c 2b 2bbinary search stl in setlower bound and upper bound binary search c 2b 2bfind by binary search c 2b 2binbulit binary searchlower bound binary search c 2b 2bbinary search c binary search recursive program cppbinary search stl 27binary search codec 2b 2b program for binary search in an arraybinary search programiz in c 2b 2bc 2b 2b binary search main binary search stl arraybinary search algorithm in c 2b 2b programbinary search c 2b 2b examplebineary search in vectorc 2b 2b binary search methodbin search cbinary search left in cimplement binary searchfunction for binary search in c 2b 2bbinary search using recursion cppbinaru searchthe function of binary search c 2b 2b syntaxhow does binary search in c 2b 2bbinary search in stlbinary search c 2b 2b algorithmbinary search c 2b 2b 5cbinary search cpp programhow to make a binary search algorithm in c 2b 2bbinary search method in c 2b 2bbinary searching cbinary search algorithm c 2b 2b codebinary search return index c 2b 2bbineary searchbinary search for particular element in list c 2b 2bbinary search tree c 2b 2b github binary search in bits 2fstdc 2b 2b hwrite a binary search function binarysearcharray for an array of ordered list of integers bin search recursively c 2b 2bcpp binary search treebinary search c 2b 2bcodebinary search using cppimplementing binary search c 2b 2bbinary and linear search in c 2b 2bc 2b 2b binary searchfind stringbinary search function stlbinary search function program in cbinary search built in cppbinary seach in c 2b 2bbinary search 2b 2bvector binarysearchbinary search program in cbinary search finction in c 2b 2bbinary searching in c 2b 2b stlbinary search in vectorbinary search function c 2b 2b array code for binary search in chow to use binary search in cppbinary search inarray c 2b 2bbinary search program in c 2b 2b stlbinry search for cppbinary search alorithm binary searcwhat is binary search c 2b 2bbinary search in bult function cppwrite a program to search an element x in an array of n integers using binary search algorithm that uses divide and conquer technique how to use binary search function in c 2b 2bbinear search in c 2b 2bc 2b 2b bimary search recursivebinary search to find 3e 3delemnetbinary search on the array in c 2b 2bhow to do binary serach in c 2b 2bbinary string search c 2b 2blinear search and binary search cppbinary searchin c 2b 2bbinary search array of strings c 2b 2btechnicspub 7c c 2b 2b algorithm series binary search algorithm and recursionbinary search tree c 2b 2b implementationbinary search code in c 2b 2bbinary search in vector cppbinary search array in cbinary search tree c 2b 2bbinary search program in c 2b 2b using classbinary serach in cconstruct binary search tree using queue in c 2b 2bhow to display binary searchingbinary saearch cpp stlbinary search c 2b 2b for a numberc 2b 2b inbuilt binary searchbinary search vector c 2b 2bbinary search implementation in c 2b 2bbinary searray binary search stl cpparray number find use of binary searchsolution mid term binary search c 2b 2bbinary search stl implementationbineary search codebinary search algorithm c 2b 2bbinary serach algorithmbinary search c 2b 2b stl return indexbinary search in vector stlbinary search c 2b 2b of given numberbinay search chow to binary search func in cppbinary search for particular elemt in list c 2b 2bc 2b 2b built in binary searchbinary search famous program in c 2b 2bcpp stl binary searchvectorwrite a program for binary search in c 2b 2bbinary search inbuilt function in c 2b 2bcall binary search c 2b 2bbinary search struct c 2b 2bbinary search examplesbsearch in vector c 2b 2bfunction binary search in c 2b 2bbinary search function cppbinary search iterative c 2b 2bbinary sreach in cbinary seachbinary search in c 2b 2b codesequential search c 2b 2bwrite a program to implement binary searchbinary search c 2b 2b codebinary search c 2b 2b vectorbinary search algorithm in c 2b 2bbinary seacrh in cppsearch algorithm in c 2b 2b stlbinaray search in the arrary in c 2b 2bbinraysearch stl gfg syntax of binary search in cppbinary search pseudocode c 2b 2bc 2b 2b algorithm stl binary searchinbuilt binary search in c 2b 2b 5cc 2b 2b code for binary searchcpp binary search stl arraybinary search in codingno matching function for call to e2 80 98binary search 28 2c std 3a 3avector 3a 3aiterator 2c int 29 e2 80 99 bool present 3d binary search 28a begin 2ca end 28 29 2c3 29 3b 2f 2ftruesearch function in stl will search forbinary search cpp codeupper bound binary search and lower bound searchbinary search algorithm code in c 2b 2b1 write a program to search the elements of an array using binary search 3 4 in javabinary search in c 2b 2b gfg multiple questionsbinary search upper boundbinarysearch in c 2b 2b stlstl binary searchbinary search in cppbinary search arraybinary search c 2b 2b cppreferencec program binary searchbinary search c 2b 2b codebinary serarch c 2b 2binbuilt binary search in c 2b 2bsearch function in c 2b 2b stlbinary search class 12th c 2b 2bc code for binary searchbinary search in c 2b 2b iterativec 2b 2b binary search function formatbest algorithm for binary search in cstl binary search functionbinary search stl to find the indexbinary search c 2b 2b function 3bbin search in cppcpp binary search functionbinary search in stl c 2b 2bfunction binary searching c 2b 2bsearch an element in an array using binary searchbinarysearch stlbinary search using stl c 2b 2bbinary search cbinary search in c 2b 2bbinary search algorithm c 2b 2b 5cbin search in cpp stlbinary search vector string c 2b 2bbinary search in string c 2b 2bcpp stl binary searchbinary search on string in c 2b 2bbinaryy search program in c 2b 2bbinary search loo 5b c 2b 2bbinary and linear search program in c 2b 2bbinsary search c 2b 2bc 2b 2b binary search structbinary search function in bits 2fstdc 2b 2bcode for binary searchc function for binary searchbinary search stdc programming binary searchbinary search in c 2b 2bbinary searcy c 2b 2bbinary search algorithm cppbinarysearch in cppbinary search string c 2b 2bimplementing binarysearch on vectorbinarysearch c 2b 2b stlbinary search in vector decreasing c 2b 2bbinary search stl on array in c 2b 2bbinary search 28 29 in c 2b 2bbinary search stlbinary searching c 2b 2bis there a binary serach function in c 2b 2bbinary search code in cppbinary search st 3blbinary search in a binary arrayexam exercise binary search c 2b 2b binary search program c 2b 2bbinary search functionpredefined binary search function in c 2b 2bbinary search stl functionbinary searc hstlbinary search combinary search in vector c 2b 2bbinary search c 2b 2b coidebinary search with algorithm c 2b 2bimplement binary search inc 2b 2bbinary serch in c 2b 2bbinary search function in cbinary search c 2b 2b stringc 2b 2b example of a binary searchhow to display binary searching c 2b 2bbinary search tree programs cppbinary search of an array cppbuilt in binary search c 2b 2bbinary search with searchbinary search function in c 2b 2b