5 %> Written by Aroh Barjatya
7 %> Binary search
for values specified in vector
'var' within data vector
'x'
8 %> The data has to be
pre-sorted in ascending or decending order
9 %> There is no way to predict how the
function will behave
if there
10 %> are multiple numbers with same value.
11 %> returns the index values of the searched numbers
13 %> @c flag_bin added by JT. @c flag_bin = 1 will cause to
return the index corresponding to the highest number in
case
14 %> of @c var being between two numbers.
20 function index =
bsearch(x,var, flag_bin)
27 [xRow xCol] = size(x);
28 if x(1) > x(xLen) % means x is in descending order
35 elseif x(1) <= x(xLen) % means x is in ascending order
38 'badly formatted data. Type ''help
bsearch\'')';
48 elseif var(i) >= x(high)
54 mid = round((low + high)/2);
57 elseif (var(i) > x(mid))
73 elseif flag_bin || ((x(low) - var(i))^2 > (x(high) - var(i))^2)
81 index = xLen - index + 1;
Pre-processing block base class.
function bsearch(in x, in var, in flag_bin)