BINARY SEARCH USING C PROGRAM





1. Write a simple code for binary search in c programming language
2. Wap a c program to search an element in an array using binary search

#include<stdio.h>
int main(){

    int a[10],i,n,m,c=0,l,u,mid;

    printf("Enter the size of an array: ");
    scanf("%d",&n);

    printf("Enter the elements in ascending order: ");
    for(i=0;i<n;i++){
         scanf("%d",&a[i]);
    }

    printf("Enter the number to be search: ");
    scanf("%d",&m);

    l=0,u=n-1;
    while(l<=u){
         mid=(l+u)/2;
         if(m==a[mid]){
             c=1;
             break;
         }
         else if(m<a[mid]){
             u=mid-1;
         }
         else
             l=mid+1;
    }
    if(c==0)
         printf("The number is not found.");
    else
         printf("The number is found.");

    return 0;
}

Sample output:
Enter the size of an array: 5
Enter the elements in ascending order: 4 7 8 11 21
Enter the number to be search: 11
The number is found.





38 comments:

  1. not working

    ReplyDelete
    Replies
    1. you try again

      Delete
  2. Hello friend ur program is working sucessfully. Thanks for it.

    ReplyDelete
  3. cud u put some comments 2 make it easier 2 undrstand

    ReplyDelete
  4. thanx bro!!! u got a good hand in C!!!!!!!!

    ReplyDelete
  5. please put some comments

    ReplyDelete
  6. hey i need a program for graphical representation of binary search in c

    ReplyDelete
  7. hey can i get a program of queue in c using graphic.c

    ReplyDelete
  8. it saves our time ....... thanx

    ReplyDelete
  9. lucky guy

    ReplyDelete
  10. only works if input array is already sorted

    ReplyDelete
  11. seocndly ..for m<a[mid]){ u=mid-1; the loop will go infinte

    ReplyDelete
  12. sorry it wont go infinite..

    ReplyDelete
  13. I think l < u would be correct

    ReplyDelete
  14. It is not working......

    ReplyDelete
  15. can show us it's algorithm as well as flowchart.

    ReplyDelete
  16. good work

    ReplyDelete
  17. for searching,array must be sorted

    ReplyDelete
  18. its realy gud thanx dear

    ReplyDelete
  19. Could always the input be in ascending order?

    ReplyDelete
    Replies
    1. Yup it should always be in ascending order.

      Delete
  20. nice work bro

    ReplyDelete
  21. thank you very much. i feel very easy in using 'c' in this blog. so simple and power

    prasad..

    ReplyDelete
  22. simple huffman algorithm
    #include
    #include
    #include
    void main()
    {
    struct huff
    {
    long int freq,code;
    char data[10];
    }h[50];
    long int n,n1=100,i,j,n2=90,temp;
    char tempc[10];
    clrscr();
    printf("\t\t\tHUFFMAN ALGORITHM\n\n");
    printf("Enter the number of character");
    scanf("%ld",&n);
    for(i=0;i<n;i++)
    {
    printf("Enter the character::");
    scanf("%s",h[i].data);
    printf("Enter the frequency for character %s::", h[i].data);
    scanf("%ld",&h[i].freq);
    }
    for(i=0;i<n-1;i++)
    {
    for(j=1;j<n;j++)
    {
    if(h[i].freq<h[j].freq)
    {
    temp=h[i].freq;
    h[i].freq=h[j].freq;
    h[j].freq=temp;
    strcpy(tempc,h[i].data);
    strcpy(h[i].data,h[j].data);
    strcpy(h[j].data,tempc);
    }
    }
    }
    h[0].code=10;
    h[1].code=11;
    for(i=2;i<n;i++)
    {
    if(i%2==0)
    {
    h[i].code=h[i-2].code+n1;
    n1=n1*10;
    }
    else
    {
    h[i].code=h[i-2].code+n2;
    n2=n2*10;
    }
    }
    printf("Huffman code");
    printf("\nchar\tfreq\tcode");
    for(i=0;i<n;i++)
    {
    printf("\n%s\t%ld\t%ld\n",h[i].data,h[i].freq,h[i].code);
    }
    getch();
    }

    ReplyDelete
  23. cud u pls do d programs fo findin hcf & lcm ?

    ReplyDelete
  24. thanx ....but pls put commands....sir

    ReplyDelete
  25. so many closed braces but only one open brace...how ca it work!!!

    ReplyDelete
  26. Thankzzzz....!!!!!
    Itzz really very useful 2 meeee......!!!!!!

    ReplyDelete
  27. Thank you dude its working thanks a lot

    ReplyDelete
  28. #include
    int main(){

    int a[10],i,n,m,c=0,l,u,mid;
    int j;

    printf("Enter the size of an array: ");
    scanf("%d",&n);

    printf("Enter the elements in ascending order: ");
    for(i=0;i<n;i++){
    scanf("%d",&a[i]);
    }

    printf("Enter the number to be search: ");
    scanf("%d",&m);

    for(j=0;j<n;j++)
    {
    if(m==a[j])
    {
    c=1;
    }

    }


    if(c==0)
    printf("The number is not found.");
    else
    printf("The number is found.");

    return 0;
    }

    ReplyDelete
  29. what is the diff between linear search and binary search

    ReplyDelete
  30. #include

    int main()
    {
    int c, first, last, middle, n, search, array[100];

    printf("Enter number of elements\n");
    scanf("%d",&n);

    printf("Enter %d integers\n", n);

    for ( c = 0 ; c < n ; c++ )
    scanf("%d",&array[c]);

    printf("Enter value to find\n");
    scanf("%d",&search);

    first = 0;
    last = n - 1;
    middle = (first+last)/2;

    while( first <= last )
    {
    if ( array[middle] < search )
    first = middle + 1;
    else if ( array[middle] == search )
    {
    printf("%d found at location %d.\n", search, middle+1);
    break;
    }
    else
    last = middle - 1;

    middle = (first + last)/2;
    }
    if ( first > last )
    printf("Not found! %d is not present in the list.\n", search);

    return 0;
    }

    ReplyDelete
  31. i am getting it a postion before. ex 1 2 3 4 and i choose to search 3 it show the postion of 3 as 2. help :)

    ReplyDelete