Answers for "binary search return index c++"

C++
1

binary search return index c++

int binarySearch(vector<int>& arr, int p, int r, int num) {
        while (p <= r) {
            int mid = (p + r)/2;
            
            if (arr[mid] == num) return mid;
            else if (arr[mid] > num) r = mid-1;
            else  p = mid+1;
        }
        return -1;
    }
Posted by: Guest on September-17-2021
2

binary search function in c++

#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;
}
Posted by: Guest on August-13-2020

Browse Popular Code Answers by Language