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 :: c++ multiple inheritance 
Cpp :: cin exceptions c++ 
Cpp :: c++ convert const char* to int 
Cpp :: cpp ifdef 
Cpp :: factorial in c++ using recursion 
Cpp :: c++ auto 
Cpp :: c++ write to csv file append 
Cpp :: getline() 
Cpp :: how to create a file in c++ 
Cpp :: C++ String Compare Example 
Cpp :: C++ Nested if...else 
Cpp :: c ++ split_string 
Cpp :: c++ math 
Cpp :: c++ data types 
Cpp :: oop in cpp 
Cpp :: sfml keyboard events cpp 
Cpp :: Translation codeforces in c++ 
Cpp :: c++ sizeof 
Cpp :: for statement c++ 
Cpp :: adding variables c++ 
Cpp :: what is the meaning of life and everything in the universe 
Cpp :: find maximum sum in array of contiguous subarrays 
Cpp :: inline function in cpp 
Cpp :: c++ write to file in directory 
Cpp :: __builtin_popcount long long 
Cpp :: order 2d array in c++ 
Cpp :: c++ class constructor variable arguments 
Cpp :: pointer to pointer c++ 
Cpp :: calling by reference c++ 
Cpp :: char input in c++ 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =