Binary Search

Binary Search

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn C programming concepts. You can learn C language at your own speed and time. One can learn concepts of C language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Binary Search
Binary Search

Write a C program to Binary Search

PROGRAM: Binary Search

/* Binary Search */
#include<stdio.h>

int main()
{
 int arr[10],i,max,min,mid,val,index;

 printf("Please enter 10 values in ascending order:\n");
 for(i=0;i<10;i++)
  scanf("%d",&arr[i]);
 
 printf("\nEnter a value to be searched: ");
 scanf("%d",&val);
 
 max=9;
 min=0;
 index=-1;
 while(min<=max)
 {
  mid=(max+min)/2;
  if(val==arr[mid])
  {
   index=mid;
   break;
  }
  if(arr[mid]>val)
   max=mid-1;
  else
   min=mid+1;
 }
 
 if(index>=0)
  printf("Value found in Array at %d location",index);
 else
  printf("Value not found in Array");
 return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *