Catfisher
0
Q:

binary search function in c++

#include <iostream>
using namespace std;

// This program performs a binary search through an array, must be sorted to work
int binarySearch(int array[], int size, int value) 
{   
    int first = 0,         // First array element       
    last = size - 1,       // Last array element       
    middle,                // Mid point of search       
    position = -1;         // Position of search value   
    bool found = false;        // Flag   
    while (!found && first <= last) 
    {      
        middle = (first + last) / 2;     // Calculate mid point      
        if (array[middle] == value)      // If value is found at mid      
    	{         
                found = true;         
                position = middle;      
        }      
        else if (array[middle] > value)  // If value is in lower half         
            last = middle - 1;      
        else         
            first = middle + 1;          // If value is in upper half   
    }   
    return position;
}
int main ()
{
    const int size = 5; // size initialization
    int array[size] = {1, 2, 3, 4, 5}; // declare array of size 10
    int value; // declare value to be searched for
    int result; // declare variable that will be returned after binary search

    cout << "What value would you like to search for? "; // prompt user to enter value
    cin >> value;
    result = binarySearch(array, size, value);

    if (result == -1) // if value isn't found display this message
        cout << "Not found\n";
    else  // If value is found, displays message
        cout << "Your value is in the array.\n"; 
  
    return 0;
}
3
#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;
}
1
// C++ code to demonstrate the working of binary_search() 
  
#include<bits/stdc++.h>  
using namespace std; 
  
int main() 
{ 
    // initializing vector of integers 
    vector<int> arr = {10, 15, 20, 25, 30, 35}; 
      
    // using binary_search to check if 15 exists 
    if (binary_search(arr.begin(), arr.end(), 15)) 
       cout << "15 exists in vector"; 
    else 
       cout << "15 does not exist"; 
       
    cout << endl; 
      
    // using binary_search to check if 23 exists 
    if (binary_search(arr.begin(), arr.end(), 23)) 
         cout << "23 exists in vector"; 
    else 
         cout << "23 does not exist"; 
       
    cout << endl;     
} 
1
#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; 
}
2
binary_search(startaddress, endaddress, valuetofind)

startaddress: the address of the first element of the array.
endaddress: the address of the last element of the array.
valuetofind: the target value which we have to search for.
2
int result = -1;
  int low = 0;
  int high = N-1; // N - # of elements
   while (low <= high)
   {  mid = (low + high) / 2;
      if ( item == vector[mid])
	  {  result = mid;
	     break; 
      }
      else if (item > vector[mid] )
	           { low =  mid + 1; }
          else  { high = mid - 1; }
   }
0

New to Communities?

Join the community