"dichotomic search c++" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "dichotomic search c++" answered properly. Developers are finding an appropriate answer about dichotomic search c++ related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like dichotomic search c++. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on dichotomic search c++. 

binary search in c++

By Helpful_HeroinHelpful_Heroin on Jul 20, 2020
#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; 
}

Add Comment

5

dichotomic search c++

By Phil the ice cream manPhil the ice cream man on Dec 09, 2020
using namespace std; 
  
// A recursive binary search function. It returns 
// location of x in given array arr[l..r] is present, 
// otherwise -1 
int binarySearch(int arr[], int l, int r, int x) 
{ 
    if (r >= l) { 
        int mid = l + (r - l) / 2; 
  
        // If the element is present at the middle 
        // itself 
        if (arr[mid] == x) 
            return mid; 
  
        // If element is smaller than mid, then 
        // it can only be present in left subarray 
        if (arr[mid] > x) 
            return binarySearch(arr, l, mid - 1, x); 
  
        // Else the element can only be present 
        // in right subarray 
        return binarySearch(arr, mid + 1, r, x); 
    } 
  
    // We reach here when element is not 
    // present in array 
    return -1; 
} 
  
int main(void) 
{ 
    int arr[] = { 2, 3, 4, 10, 40 }; 
    int x = 10; 
    int n = sizeof(arr) / sizeof(arr[0]); 
    int result = binarySearch(arr, 0, n - 1, x); 
    (result == -1) ? cout << "Element is not present in array"
                   : cout << "Element is present at index " << result; 
    return 0; 
} 

Add Comment

0

All those coders who are working on the C++ based application and are stuck on dichotomic search c++ can get a collection of related answers to their query. Programmers need to enter their query on dichotomic search c++ related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about dichotomic search c++ for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of dichotomic search c++ while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "dichotomic search c++". Visit this developer's friendly online web community, CodeProZone, and get your queries like dichotomic search c++ resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "dichotomic search c++"

View All C++ queries

C++ queries related to "dichotomic search c++"

Browse Other Code Languages

CodeProZone