mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Update Binary Search.cpp
This commit is contained in:
parent
1eef89591e
commit
2f6208532b
@ -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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user