Update Binary Search.cpp

This commit is contained in:
Faizan Ahamed 2020-04-26 14:42:55 +05:30 committed by GitHub
parent 1eef89591e
commit 2f6208532b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,19 +13,19 @@ int binary_search(int a[], int l, int r, int key) {
} }
int main(int argc, char const *argv[]) { int main(int argc, char const *argv[]) {
int n, key; int n, key;
cout << "Enter size of array: "; std::cout << "Enter size of array: ";
cin >> n; std::cin >> n;
cout << "Enter array elements: "; std::cout << "Enter array elements: ";
int* a = new int[n]; int* a = new int[n];
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
cin >> a[i]; std::cin >> a[i];
} }
cout << "Enter search key: "; std::cout << "Enter search key: ";
cin >> key; std::cin >> key;
int res = binary_search(a, 0, n - 1, key); int res = binary_search(a, 0, n - 1, key);
if (res != -1) if (res != -1)
cout << key << " found at index " << res << endl; std::cout << key << " found at index " << res << endl;
else else
cout << key << " not found" << endl; std::cout << key << " not found" << endl;
return 0; return 0;
} }