fastsearch

Search an element in the array, or the next smaller and greater
1.8K Downloads
Updated 9 Dec 2008

View License

Find the value (wert) in the in ascending order sorted array (array) with
fastsearch-algorithm.

function index = fastsearch (array, von, bis, wert, bias)

Input
array: array to search in
von: lower border of the search region, should be set to 1.
bis: higher border of the search region, should be set to
length(array). These variables cannot be initialize within the
function because of the recursion.
wert: element to search for
bias: 0: search exact the element
+1: search the element or the next greater
-1: search the element or the next smaller
Output
index: index of the element in array;
-1 if not found

The algorithm is very quick, check the time below:
--------------------
>> a=rand(1,10^7);
b=sort(a);
tic;c=fastsearch(b,1,length(b),b(123456),1);toc
Elapsed time is 0.000795 seconds.
>> c
c =
123456
--------------------
>> tic;c=fastsearch(b,1,length(b),0.16234,-1);toc
Elapsed time is 0.014674 seconds.
>> 0.16234-b(c)
ans =
7.6316e-008
>> 0.16234-b(c+1)
ans =
-7.0494e-008

Cite As

Valentin Kuklin (2026). fastsearch (https://www.mathworks.com/matlabcentral/fileexchange/16976-fastsearch), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Characters and Strings in Help Center and MATLAB Answers
Version Published Release Notes
1.1.0.0

The bug noticed by Dru Franworth (thanks) is fixed.

1.0.0.0

The improvement is recommended by Urs (us) Schwarz.
The function is called now as
fastsearch (array, wert, bias).