Search C Program | nimishsoft@gmail.com

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;
}


Related Posts:
-------------------------------------------------------------
Linear Search
http://cbasicprogram.blogspot.in/2012/05/linear-search-in-array.html 

4 comments:

  1. For More Details of binary search
    plz visit us on
    https://olevelprogramming.blogspot.com/2019/04/binary-search-c-program.html

    ReplyDelete
  2. Write a program which accept the elements of array and find the count of elements which is divisible of 6 and 8

    ReplyDelete