IT HOME Programming care

IT HOME Programming Care

Monday, June 27, 2016

Binary search in C Program

Binary search in C Programming

Binary Search with data Structure in C Programming


#include<stdio.h>
main(){
    int i,k,n,mid,flag=0;
    printf("Enter the total series: ");
    scanf("%d",&n);
    int ar[n];

    printf("Enter the series value in Ascending Order:   ");
    for(i=0;i<n;i++){
        scanf("%d",&ar[i]);
        if(i<n-1){
            printf("\nEnter the next series value: ");
        }
    }

    printf("\nThe series value are : ");
    for(i=0;i<n;i++){
        printf("  %d",ar[i]);
    }

    printf("\nEnter Your Finding value: ");  scanf("%d",&k);
    int start=0;
    int End=n-1;

    while(start<=End){
        mid=(start+End)/2;
        if(ar[mid]==k){
            printf("\n%d is in %d no. index : ",k,mid+1);
            flag=1;
            break;
        }
        else if(ar[mid]<k){
            start=mid+1;
        }
        else if(ar[mid]>k){
             End=mid-1;
        }
    }

    if(flag==0){
         printf("\nThe value is not Exist\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