Search a data using binary search
This post content:-
    
Algorithm
Flowchart
C code
Output
Pdf notes
Algorithm:-
Step 1:- start.
Step 2:- declare a sorted array.
Step 3:- declare the size of the given array.
Step 4:- declare the variable to find the number in the given array.
Step 5:- make a function for binary search algorithm.
int binarysearch(int arr[],int size,int element)
{
    int low=0;
    int high=size -1;
    while(low<=high)
    {
        int mid = (low + high)/2;
        if (arr[mid]==element)
        {
            return mid;
        }
        if (arr[mid]< element)
        {
            low = mid+1;
        }
        if (arr[mid]> element)
        {
            high = mid-1;
        }
    }
    return -1;
}
Step 6:- print result.
Step 7:- END.
Flowchart:-
C code:-
Output:-
Try it yourself :- Online c compiler
 
   
   
   
 
 
Comments
Post a Comment