Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int binSearch(int arr[], int l, int r, int x) {
- while (l <= r) {
- int m = l + (r - l) / 2;
- if (arr[m] == x)
- {
- return m;
- }
- if (arr[m] < x) {
- l = m + 1;
- } else {
- r = m - 1;
- }
- }
- return false;
- }
- int main() {
- int arr[10], x, res;
- cout<<"Enter array elements: "<<endl;
- for (int i=0; i<10; i++) {
- cin>>arr[i];
- }
- cout<<"\nEnter element to search: ", cin>>x;
- if (res=binSearch(arr, 0, 9, x), res) {
- cout<<"\nElement found at index: "<<res;
- } else {
- cout<<"\nElement not found!";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement