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 :: update variable in const function C++ 
Cpp :: c++ standard library source 
Cpp :: c++ how to read from a file 
Cpp :: c++ remove numbers from vector if larger than n 
Cpp :: c++ remove last character from string 
Cpp :: hello world program in c++ 
Cpp :: c++ int 
Cpp :: c++ split string by several space 
Cpp :: c++ inherit from class template 
Cpp :: sort vector in reverse order c++ 
Cpp :: c++ power 
Cpp :: how to sort in descending order in c++ 
Cpp :: c++ print binary treenode 
Cpp :: deep copy c++ 
Cpp :: rand() c++ 
Cpp :: concatenate two vectors c++ 
Cpp :: how to set a variable to infinity in c++ 
Cpp :: length of string in c++ 
Cpp :: new float array c++ 
Cpp :: insert image using set atribute 
Cpp :: Search Insert Position leetcode solution in cpp 
Cpp :: c++ operator overloading 
Cpp :: print counting in c++ 
Cpp :: print hello world in c++ 
Cpp :: c++ exceptions 
Cpp :: strring length in c++ 
Cpp :: stl function to reverse an array 
Cpp :: string comparison c++ 
Cpp :: print in c ++ 
Cpp :: convert ascii char value to hexadecimal c++ 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =