Skip to content

Instantly share code, notes, and snippets.

@TheRoyalDebugger
Last active October 21, 2017 14:49
Show Gist options
  • Select an option

  • Save TheRoyalDebugger/ce45e1744e65916bd72fa02c4bc3f3c2 to your computer and use it in GitHub Desktop.

Select an option

Save TheRoyalDebugger/ce45e1744e65916bd72fa02c4bc3f3c2 to your computer and use it in GitHub Desktop.

Revisions

  1. TheRoyalDebugger revised this gist Oct 21, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions BinarySearch.cpp
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,7 @@ using namespace std;

    int main(){
    int element;
    //Array must be sorted
    int arrData[] = {10,20,30,144,232,353};
    cout<<"Enter element to search:"<<endl;
    cin>>element;
  2. TheRoyalDebugger created this gist Oct 21, 2017.
    37 changes: 37 additions & 0 deletions BinarySearch.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #include<iostream>
    using namespace std;

    bool isBinarySearchFoundElement(int* arrData,int searchElement,int left, int right){
    if(left > right){
    return false;
    }
    int mid = left + (right-left)/2;
    if(arrData[mid] == searchElement){
    return true;
    }else if(searchElement < arrData[mid]){
    isBinarySearchFoundElement(arrData,searchElement,left,mid-1);
    }else{
    isBinarySearchFoundElement(arrData,searchElement,mid+1,right);
    }
    return false;
    }

    bool isFoundData(int* arrData,int element){
    int right = sizeof(arrData)/sizeof(arrData[0]);
    bool isDataFound = isBinarySearchFoundElement(arrData,element,0,right);
    return isDataFound;
    }

    int main(){
    int element;
    int arrData[] = {10,20,30,144,232,353};
    cout<<"Enter element to search:"<<endl;
    cin>>element;
    bool isFound = isFoundData(arrData,element);
    if(isFound == true){
    cout<<"Element found in array!"<<endl;
    }else{
    cout<<"Element does not exist in array!";
    }
    return 0;
    }