Saturday, August 21, 2010

Article | C Program For Binary Search

C- Program For Binary Search In C, SImple Basic Logic. Without USing any string Functions



  1. #include<stdio.h>

  2. int main(){

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

  4. printf("Enter the size of an array->");

  5. scanf("%d",&n);

  6. printf("\nEnter the elements of the array->");

  7. for(i=0;i<n;i++){

  8. scanf("%d",&a[i]);

  9. }

  10. printf("\nThe elements of an array are->");

  11. for(i=0;i<n;i++){

  12. printf(" %d",a[i]);

  13. }

  14. printf("\nEnter the number to be search->");

  15. scanf("%d",&m);

  16. l=0,u=n-1;

  17. while(l<=u){

  18. mid=(l+u)/2;

  19. if(m==a[mid]){

  20. c=1;

  21. break;

  22. }

  23. else if(m<a[mid]){

  24. u=mid-1;

  25. }

  26. else

  27. l=mid+1;

  28. }

  29. if(c==0)

  30. printf("\nThe number is not in the list");

  31. else

  32. printf("\nThe number is found");

  33. return 0;

  34. }




Loading...

Comments :

0 comments to “ C Program For Binary Search ”

Top Views

BloggerTipsTemplates

CHICKLETS

Enter your email address:

Delivered by FeedBurner

Followers