dichotomic search c 2b 2b

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

showing results for - "dichotomic search c 2b 2b"
Gael
10 Mar 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}
Davide
06 Mar 2019
1using namespace std; 
2  
3// A recursive binary search function. It returns 
4// location of x in given array arr[l..r] is present, 
5// otherwise -1 
6int binarySearch(int arr[], int l, int r, int x) 
7{ 
8    if (r >= l) { 
9        int mid = l + (r - l) / 2; 
10  
11        // If the element is present at the middle 
12        // itself 
13        if (arr[mid] == x) 
14            return mid; 
15  
16        // If element is smaller than mid, then 
17        // it can only be present in left subarray 
18        if (arr[mid] > x) 
19            return binarySearch(arr, l, mid - 1, x); 
20  
21        // Else the element can only be present 
22        // in right subarray 
23        return binarySearch(arr, mid + 1, r, x); 
24    } 
25  
26    // We reach here when element is not 
27    // present in array 
28    return -1; 
29} 
30  
31int main(void) 
32{ 
33    int arr[] = { 2, 3, 4, 10, 40 }; 
34    int x = 10; 
35    int n = sizeof(arr) / sizeof(arr[0]); 
36    int result = binarySearch(arr, 0, n - 1, x); 
37    (result == -1) ? cout << "Element is not present in array"
38                   : cout << "Element is present at index " << result; 
39    return 0; 
40} 
queries leading to this page
array binary search algorithm c 2b 2busing iterative binary search in a survey in c 2b 2bbinary search code c 2b 2bbinary search algorithm c 2b 2bbinary search in array in cc 2b 2b binary search codebinary search function c 2b 2bprograms on binary searchbinary search element in c 2b 2bcpp binary search stlbinary search in function c 2b 2bbinary search in c 2b 2b stlbinary search tree c 2b 2b codebinary search c 2b 2b of given numberbinary sort in c 2b 2bbinary search for particular elemt in list c 2b 2bc 2b 2b binary search functionbinary search snippet cpprecursive binary search c 2b 2bbinary search cppc 2b 2b binary search stlbinary search stl c 2b 2bbinary search array c 2b 2balgorithm c 2b 2b binary searchbineary search in c 2b 2bwrite a program to search a key in a list using binary searchbinary search using recursion in cppsearch function in random binary search tree in c 2b 2bbinary search iterative in cppbinary sbinary search function cppbinary search function in c 2b 2b stlbinary search iterative c 2b 2bbinary search com 22feedback 22binary search c 2b 2b codebinary search recursive c 2b 2bbinary search program in cppbinary search in cpp programbinary sarchbinary search c 2b 2b stldichotomic search c 2b 2bbinary search algorithm in c 2b 2bcode c 2b 2b exercises binary searchfind by binary search c 2b 2bbinary seacrh in cppbinary search c binary search code cppbinary search using c 2b 2b thumbnail pngbinary search recursive program cppbinary search exampe c 2b 2bc 2b 2b program for binary search in an arraycpp binary search stl arraywrite a program to search a number using binary search c 2b 2b code for binary searchrecursive function for binary search in cppbinary search cpp codecpp program to search an element in an array using recursive binary searcghhow to code binary search in c 2b 2bbinary search in c 2b 2b gfg multiple questionsbinary search for elementbinary search in cppbinary search left in cbinary searc cbinary search c 2b 2b codehow to use binary search in c 2b 2bbinary search using recursion cppinbuilt binary search in c 2b 2bbineary search c 2b 2bc 2b 2b binary serachbinary searching in array c 2b 2bbest algorithm for binary search in cbinary search c 2b 2b function 3bbin search in cppbinary search cpp programbinary search c 2b 2b in whilebinary search simple way c 2b 2bbinary search function in c 2b 2bbinary search c 2b 2bcstlbinary search algorithmin c 2b 2bbinary search method in c 2b 2bbinary search algorithm c 2b 2b 5cbinary search in c 2b 2bbinary searching cbinary search algorithm c 2b 2b codebinary search sort javabinary search for particular element in list c 2b 2bbinary search in 2b 2bbinaryy search program in c 2b 2bbinary search tree c 2b 2b github implementation binary search cbinary search algorithm pythonbinay searchc 2b 2b binary searchhow to do a binary search in c 2b 2bbinary seach cppbinary search in array c 2b 2b examplebinsary search c 2b 2bc 2b 2b binary search structbinary and linear search in c 2b 2bcode for binary searchbinary search in cbinary search c 2b 2b sc function for binary searchbinray search in c 2b 2bimplement binary search for array in c 2b 2bbinary search c 2b 2b stlc programming binary searchbinary search in c 2b 2bbinary search program in cbbinary searach code in c 2b 2bbinary searching c 2b 2bbinary searching in c 2b 2b stlcode for binary search in cbinary serach in c 2b 2bwhenever i use binary search in c 2b 2b it returns zerohow to use binary search in cppbinary search in cppbinary sort c 2b 2bbinary search in an array in cbinary search alorithm binary searching c 2b 2bc 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 in c coderecursive binary search in c 2b 2bbinary search stl 3bimplement binary search in c 2b 2bbinear search in c 2b 2bcan binary search search string c 2b 2bbinary search in recursion c 2b 2bbinary search in c programexam exercise binary search c 2b 2b binary search c 2b 2bc 2b 2b binary search algorithmbinary search program c 2b 2bbinary search in cpp stlstd binary search with comparatorc 2b 2b bimary search recursivebinary search functionbinary search cppbinary search on the array in c 2b 2bhow to binary search in cpppyhton c code for binary searchbinary search program in c 2b 2bstl binary search c 2b 2bbinary search implementation c 2b 2bbinary search algorithm c 2b 2bbinary string search c 2b 2bcode c 2b 2b exercices binary searchbinary search combinary search tree c 2b 2b implementationbinary search in c 2b 2bbinary search code in c 2b 2bbinary search c 2b 2bimplement binary search inc 2b 2bbinary search example in javabinary search sort program in c 2b 2bbinary search array in cbinary search simple program in cppbinary serch in c 2b 2bbinary search function in chow to display binary searching c 2b 2bconstruct binary search tree using queue in c 2b 2bbinary search array cppbinary search recursive c 2b 2b codehow to display binary searchingbinary search for array of element in cppc 2b 2b inbuilt binary searchbinary search in array c 2b 2bsorted search function c 2b 2bbinary search implementation in c 2b 2b binary search c 2b 2b syntaxbinary search of an array cpparray number find use of binary searchbinary search of array in chow to make a binary search algorithm c 2b 2bdichotomic search c 2b 2b