printf("searching element %d at address=%d",arr[search],arr+search);
if(arr[search] == i)//element to be searched is i .....arr[search] is array in which element is being sear//ched arr[0]==14 arr[1]==14 arr[2]==14 whereever we find 14 in array we will print that value of search..
{
printf("element searched at %d pos at address=%d",search,arr+search);
/*binary search ---------to search element in array or pointer*/
*condition for binary search is that... u have to use sorted array & u should know size of array in which element is to be searched otherwise do so/rting 1st*/
#include<stdio.h>
int main()
{
int search;
int hi=10,lw=0,mid=0,i,k,proceed;
int arr[10];//{3,5,6,7,9,10,13,14,16,17};
for(k=0;k<10;k++)
{
printf("\nGIVE ME ARRAY:");
scanf("%d",&arr[k]);
}
i=hi;//now i=10
for(proceed=0;i>1;proceed++)//int then cond check ....back......1st it inc.then check cond.
{
i=i/2;//proceed=0 i=10 ;proceed=1 10/2=5;proceed=2 5/2=2;proceed=3 2/2=1;i<1 failed out of loop
//pro=3
}
printf("\nproceed=%d",proceed);//value of procceed calculated will tell us how many no. of max.iterations will required while searching a perticular elements ;it totally depends on no. of elements r present in array..
do
{
printf("\nenter the element to be searched:");
scanf("%d",&search);
mid=0;
hi=10;lw=0;//for 2nd,3rd or more no. of element searching hi should be start by 10 & lw should be 0
mid=(hi+lw)/2;//mid =5 or again & again started from 5
for(i=0;i<proceed;i++)//loop should operate at 0,1& 2 times.
{
//mid=(hi+lw)/2;
if(arr[mid]>search)//1st time arr[5]>search
{
hi=mid;//if mid element is greater then element to be searched ;it means element to be searched will found in lower half part of array;then lw=0 as it is & mid is to be made hi
mid=(lw+hi)/2;//again mid is determined to search element untill element is found