Algorithm BinSearch(a,n,x)
//given an array a(i,10 of elements in non-decreasing.
//order, 1<=i<=1, determine whether x is present and
//if so, return j such that x = a[j], else return 0.
{
low = 1;high = n;
while(low ≤ high) do
{
mid = [(low+high)/2];
if(x<a[mid]) then high = mid-1;
else if(x>a[mid]) then low = mid+1;
else return mid;
}
return 0;
}
//given an array a(i,10 of elements in non-decreasing.
//order, 1<=i<=1, determine whether x is present and
//if so, return j such that x = a[j], else return 0.
{
low = 1;high = n;
while(low ≤ high) do
{
mid = [(low+high)/2];
if(x<a[mid]) then high = mid-1;
else if(x>a[mid]) then low = mid+1;
else return mid;
}
return 0;
}
No comments:
Post a Comment