Search
 
SCRIPT & CODE EXAMPLE
 

CPP

binary search c++

#include <vector>
using namespace std;

int binarySearch(vector<int> v, int x) {
	int l = 0, r = v.size() - 1, mid;

	while (l <= r) { 
		mid = l + (r - l) / 2;
		if (v[mid] == x) return mid;
		else if (v[mid] > x) {
			r = mid - 1;
		}
		else if(v[mid] < x) {
			l = mid + 1;
		}
	}
	return -1;
} 
Comment

how to do binary search in c++ using STL

// BY shivam kumar KIIT
#include<bits/stdc++.h>
usind namespace std;
int main()
{
	int arr[]={10,2,34,2,5,4,1};
  	sort(arr,arr+7);//sort array in ascending order before using binary search
  	binary_search(arr,arr+7,10);//return 1 as element is found
  	binary_search(arr,arr+7,3);//return 0 as element is not found
  	return 0;
}
Comment

c++ binary search

int binarySearch(arr, low, high, key) {
    if (high >= low) {
        int mid = low + (high - low) / 2; 
        if (arr[mid] == key) return mid; 
        else if (arr[mid] > key) return binarySearch(arr, low, mid - 1, key); 
        else return binarySearch(arr, mid + 1, high, key); 
    }
    return -1; 
}
Comment

c++ binary search

#include<bits/stdc++.h>
using namespace std;
int main(){
	
	//lets do it
	
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++) cin>>arr[i];
    int target;
    cin>>target; // target=?
    int l=0,h=n-1;
    int index=-1;
    while(l<=h)
    {
    	int mid=(l+h)/2;
    	if(arr[mid]==target){
    		index=mid;
    		break;
		}else if(arr[mid]<target) l=mid+1;
		else h=mid-1;
		
	}
	cout<<"NUMBER FOUND AT INDEX-"<<index<<endl;
	return 0;
}
Comment

binary search c++

int binarySearch(int arr[], int l, int r, int x)
{
    if (r >= l) {
        int mid = l + (r - l) / 2;
        if (arr[mid] == x)
            return mid;
        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);
        return binarySearch(arr, mid + 1, r, x);
    }
    return -1;
}
Comment

Binary Search in C++

// Binary Search in C++

#include <iostream>
using namespace std;

int binarySearch(int array[], int x, int low, int high) {
  
	// Repeat until the pointers low and high meet each other
  while (low <= high) {
    int mid = low + (high - low) / 2;

    if (array[mid] == x)
      return mid;

    if (array[mid] < x)
      low = mid + 1;

    else
      high = mid - 1;
  }

  return -1;
}

int main(void) {
  int array[] = {3, 4, 5, 6, 7, 8, 9};
  int x = 4;
  int n = sizeof(array) / sizeof(array[0]);
  int result = binarySearch(array, x, 0, n - 1);
  if (result == -1)
    printf("Not found");
  else
    printf("Element is found at index %d", result);
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: string length in c++ 
Cpp :: gettimeofday header file 
Cpp :: c++ contains 
Cpp :: convert characters to lowercase c++ 
Cpp :: c define 
Cpp :: classes constructor in c++ 
Cpp :: c++ string conversion operator 
Cpp :: convert integer to string in c++ 
Cpp :: how can we create 4 digit random number in c++ 
Cpp :: convert kelvin to Fahrenheit 
Cpp :: check if element in dict c++ 
Cpp :: c++ float and double 
Cpp :: insert in vector 
Cpp :: comparing characters of a string in c++ 
Cpp :: c++ auto 
Cpp :: c++ get pointer from unique_ptr 
Cpp :: log base 2 in c++ 
Cpp :: enum c++ 
Cpp :: break statement in c++ program 
Cpp :: c++ inheritance constructor 
Cpp :: C++ Calculating the Mode of a Sorted Array 
Cpp :: set of vectors c++ 
Cpp :: sweetalert2 email and password 
Cpp :: max c++ 
Cpp :: vector::at() || Finding element with given position using vector in C++ 
Cpp :: input c++ 
Cpp :: c++ linked list delete node 
Cpp :: STD::ERASE FUNCTION IN C++ 
Cpp :: web dev c++ 
Cpp :: c++ function pointer 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =