From 6cf8942d01992e1bda41ae05955c83db09790b72 Mon Sep 17 00:00:00 2001 From: abhinay9601 Date: Mon, 4 Oct 2021 16:34:22 +0530 Subject: [PATCH] binary search added --- binary_search.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 binary_search.cpp diff --git a/binary_search.cpp b/binary_search.cpp new file mode 100644 index 00000000..da9c0f42 --- /dev/null +++ b/binary_search.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; +int main() +{ + int A[] = {6, 8, 13, 17, 20, 25, 28, 31, 38, 46}; + int l = 0, h = 9, key, mid; + cout << "Enter key:- "; + cin >> key; + while (l <= h) + { + mid = (l + h) / 2; + if (key == A[mid]) + { + cout << "Found at :- " << mid; + return 0; + } + else if (key < A[mid]) + h = mid - 1; + else + l = mid + 1; + } + cout << "Not Found"; + return 0; +} \ No newline at end of file