CCCPaste

Binary Search program

// 2) WAP to implement binary search
#include 
using namespace std;

int main(){
    
    cout<<"Enter the size of the sorted array: ";
    int n;
    cin>>n;
    int arr[n];
    cout<<"Enter the array elements: ";
    for(int i=0;i>arr[i];
    }

    cout<<"Enter the element to be searched: ";
    int element;
    cin>>element;
    int index=-1;
    int low=0,high=n-1;
    while(low<=high){
        int mid=(low+high)/2;
        if(arr[mid]==element){
            index=mid;
            break;
        }
        else if(arr[mid]>element) high=mid-1;
        else low=mid+1;
    }

    if(index!=-1){
        cout<<"Element found at index: "<