mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
5dbd45a3f0
in line 23. int a[n]; give error while running i.e. expression must have a constant value. so, rectifying this we can use pointer. int* a = new int[n];
43 lines
801 B
C++
43 lines
801 B
C++
#include <iostream>
|
|
using namespace std;
|
|
int binary_search(int a[], int l, int r, int key)
|
|
{
|
|
while (l <= r)
|
|
{
|
|
int m = l + (r - l) / 2;
|
|
if (key == a[m])
|
|
return m;
|
|
else if (key < a[m])
|
|
r = m - 1;
|
|
else
|
|
l = m + 1;
|
|
}
|
|
return -1;
|
|
}
|
|
int main(int argc, char const *argv[])
|
|
{
|
|
int n, key;
|
|
cout << "Enter size of array: ";
|
|
cin >> n;
|
|
cout << "Enter array elements: ";
|
|
/*
|
|
int a[n];
|
|
getting error size must be declare
|
|
so for rectifying this use pointer.
|
|
int a[n]; replace by int* a = new int[n];
|
|
*/
|
|
int* a = new int[n];
|
|
for (int i = 0; i < n; ++i)
|
|
{
|
|
cin >> a[i];
|
|
}
|
|
cout << "Enter search key: ";
|
|
cin >> key;
|
|
int res = binary_search(a, 0, n - 1, key);
|
|
if (res != -1)
|
|
cout << key << " found at index " << res << endl;
|
|
else
|
|
cout << key << " not found" << endl;
|
|
return 0;
|
|
}
|