diff --git a/Competitive programming/C++/Binary search.cpp b/Competitive programming/C++/Binary search.cpp new file mode 100644 index 0000000..2f38c41 --- /dev/null +++ b/Competitive programming/C++/Binary search.cpp @@ -0,0 +1,39 @@ +//binary +#include +using namespace std; + +int main () +{ +int n, i, s, l, mid, element; +cout<<"Enter size "<> n ; + int a[n]; +cout<<"Enter the elements in ascending order"<> a[i]; + cout<<"Enter the value you want to search"<>element; + + s = 0; + l = n - 1; + while (s <= l) + { + mid = (s + l) / 2; + if (a[mid] == element) +{ + cout <<"Element is present at "< element) + l = mid - 1; + else + s = mid + 1; + } + + if (a[mid] != element){ + cout << "Search element not found"; + +} + + return 0; +}