binary serach in c++
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;
}
Also in C++:
- Title
- how to make a heap using stl in c++
- Category
- C++
- Title
- binary serach in c++
- Category
- C++
- Title
- cpp create lambda with recursion
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- mingw32/bin/ld.exe: C:\Users\mfrom\AppData\Local\Temp\ccSKcRks.o:PizzaPi.cpp:(.text$_ZN5PizzaC2Ev[__ZN5PizzaC2Ev]+0xa): undefined reference to `vtable for Pizza' collect2.exe: error: ld returned 1 exit status
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- C++ Syntax
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- dynamic 2d array c++
- Category
- C++
- Title
- max element in array c++ stl
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- floor() in c++
- Category
- C++
- Title
- cin.ignore
- Category
- C++
- Title
- reverse a linked list using recursion
- Category
- C++
- Title
- do while loop c++
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- how to sort a vector in c++
- Category
- C++
- Title
- how to convert n space separated integers in c++
- Category
- C++
- Title
- arrays in C++
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- SFML window
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- how to define a while statement in c++
- Category
- C++
- Title
- c++ enum rand
- Category
- C++
- Title
- cut by delimiter c++
- Category
- C++
- Title
- what does the modularity mean in c++
- Category
- C++
- Title
- what does count function do in hashmap
- Category
- C++
- Title
- expected initializer before 'isdigit'|
- Category
- C++
- Title
- convert decimal to binary in c++
- Category
- C++
- Title
- set in c++
- Category
- C++
- Title
- how to pass an object by reference in c++
- Category
- C++
- Title
- delete files c++
- Category
- C++
- Title
- sleep system function linux c++
- Category
- C++
- Title
- border radius layout android xml
- Category
- C++
- Title
- correct sequence of compilation process in c++
- Category
- C++
- Title
- set precision in c++
- Category
- C++
- Title
- class is replace by structure
- Category
- C++
- Title
- c++ sql
- Category
- C++
- Title
- can we compare a long long int with int in c++ using max or min functions
- Category
- C++
- Title
- class in c++
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- how to make a 2d vector in c++
- Category
- C++
- Title
- c++ variable argument
- Category
- C++
- Title
- C++ If
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- how to use winmain function
- Category
- C++
- Title
- file reading c++
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- ue4 c++ enum
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- max in c++
- Category
- C++
- Title
- new in c++
- Category
- C++
- Title
- GetCurrentThreadId c
- Category
- C++
- Title
- c++ functions
- Category
- C++
- Title
- coping 2d vector in cpp
- Category
- C++
- Title
- how to compile opencv c++ in ubuntu
- Category
- C++
- Title
- how to get os name in c++
- Category
- C++
- Title
- double to int c++
- Category
- C++
- Title
- c++ program to input and print text using Dynamic Memory Allocation.loop
- Category
- C++
- Title
- length of string c++
- Category
- C++
- Title
- how to declare a function in c++
- Category
- C++
- Title
- getline in c++
- Category
- C++
- Title
- how to dynamically allocate an array c++
- Category
- C++
- Title
- c++ class method example
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- cpp nan value
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- how to find the mode of a vector c++
- Category
- C++
- Title
- how to decalre a string in c++
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- equal elements in two arrays in c++
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- c++ create object
- Category
- C++
- Title
- index string c++
- Category
- C++
- Title
- modular exponentiation c++
- Category
- C++
- Title
- print type cpp
- Category
- C++
- Title
- typeid to string c++
- Category
- C++
- Title
- c++ template function
- Category
- C++
- Title
- min heap priority queue c++
- Category
- C++
- Title
- gcd in c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- calling by reference and pointers c++
- Category
- C++
- Title
- sqrt cpp
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- removing repeated characters in a string c++
- Category
- C++
- Title
- queue stl c++
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- sort a string alphabetically c++
- Category
- C++
- Title
- font awesome bootstrap cdn
- Category
- C++
- Title
- lambda c++
- Category
- C++