MAGMA Interview Question
Software Engineer / DevelopersHere is the Single Comparision Code:
int BinSearch(int A[], int n , int key )
{
int s = 0;
int e = n-1;
int m;
while(s<e)
{
m = (s+e)/2;
if(key<=A[m])
{
e = m;
}
else
{
s = m+1;
}
}
if(A[e] == key)
{
printf("Key %d present at location %d\n", key, e);
return e;
}
else
{
printf("Key Not Present in List\n");
return -1;
}
}
1. Generally in BinarySearch algorithm we do 2 comparisions
2. He asked me to use single comparision in While loop .
3. code for this .
- siva.sai.2020 February 17, 2011