IT HOME Programming care

IT HOME Programming Care

Sunday, January 29, 2017

Binary search in C Programming

http://ithomeprogrammingcare.blogspot.com/2017/01/blog-post.html
Binary Search in C Programming


//Binary search in C Programming
#include<stdio.h>
main()
{
int i,findVal,n,mid,flag=0;
printf("Enter the total series: ");
scanf("%d",&n);
int sr[n];
printf("Enter the series value in Ascending Order: ");
for(i=0;i<n;i++)
{
    //series value input
    scanf("%d",&sr[i]);
    //ask next value
    if(i<n-1)
    printf("\nEnter the next series value: ");
}
    //display the series value
    printf("\nThe series value are : ");
    for(i=0;i<n;i++){
        printf(" %d",sr[i]);
    }
    //ask which value want to find
    printf("\nFinding value: ");
    scanf("%d",&findVal);
    //Binary search
    int start=0;
    int End=n-1;
    while(start<=End)
    {
        mid=(start+End)/2;
        if(sr[mid]==findVal){
           printf("\n%d is in %d index : ",findVal,mid+1);
           flag=1;
           break;
        }
    /*whether finding value is grater then middle position value*/
        else if(sr[mid]<findVal){
            start=mid+1;
        }
        /*whether finding value is Less then middle position value*/
        else if(sr[mid]>findVal){
            End=mid-1;
        }
    }
    //check value is founded or not
    if(flag==0){
        printf("\nThe value is not Founded\n");
    }
}

0 comments:

Post a Comment

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();

IT HOME Freelancing Care