Binary Operator
Binary Operator
The following searching operations for a Key value in a given list of integers :
ii) Binary search*/
#include <stdio.h>
#define MAX_LEN 10
/* Non-Recursive function*/
Void b_search_nonrecursive(int l[],int num,int ele)
{
Int l1,I,j, flag = 0;
L1 = 0;
I = num-1;
While(l1 <= i)
{
J = (l1+i)/2;
If( l[j] == ele)
{
Printf(“\nThe element %d is present at position %d in list\n”,ele,j);
Flag =1;
Break;
}
Else
If(l[j] < ele)
L1 = j+1;
Else
I = j-1;
}
If( flag == 0)
Printf(“\nThe element %d is not present in the list\n”,ele);
}
/* Recursive function*/
Int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a)
{
Int m,pos;
If (arrayStart<=arrayEnd)
{
M=(arrayStart+arrayEnd)/2;
If (l[m]==a)
Return m;
Else if (a<l[m])
Return b_search_recursive(l,arrayStart,m-1,a);
Else
Return b_search_recursive(l,m+1,arrayEnd,a);
}
Return -1;
}