Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,14 @@
function i = binsearch_r(array, val, low, high)
if ( high < low )
i = 0;
else
mid = floor((low + high) / 2);
if ( array(mid) > val )
i = binsearch_r(array, val, low, mid-1);
elseif ( array(mid) < val )
i = binsearch_r(array, val, mid+1, high);
else
i = mid;
endif
endif
endfunction

View file

@ -0,0 +1,16 @@
function i = binsearch(array, value)
low = 1;
high = numel(array);
i = 0;
while ( low <= high )
mid = floor((low + high)/2);
if (array(mid) > value)
high = mid - 1;
elseif (array(mid) < value)
low = mid + 1;
else
i = mid;
return;
endif
endwhile
endfunction

View file

@ -0,0 +1,4 @@
r = sort(discrete_rnd(10, [1:10], ones(10,1)/10));
disp(r);
binsearch_r(r, 5, 1, numel(r))
binsearch(r, 5)