I have an ordered list that I am trying to search but am not looking for a specific value in the list. I have a threshold and want to find all the values greater than that value.
For example, if I have the list 2 4 6 8 10 12 14 16 18 20, I'd be searching for the index that returns me all the values greater than a specific number like 13.
Here is what my code looks like, but it isn't working properly for all cases. For example, if I have a list of 2 20 and looking for numbers bigger than 10 the code gets stuck. I'm trying to find an elegant solution to these problem without adding in a bunch of if loops to check for specific cases as I will be running this function 10 million times on lists up to 8! long, so efficiency is key. Any suggestions would be appreciated.
% Note that permutations represents my ordered list of values
function [index] = binarySearch(permutations, threshold)
start = 1;
stop = length(permutations);
middle = floor((stop+start)/2);
while(stop-start > 0)
if(permutations(middle) > threshold)
stop = middle;
elseif(permutations(middle) < threshold)
start = middle;
elseif(permutations(middle) == threshold)
index = middle;
return;
end
if(stop == 1)
index = 0;
return;
end
middle = floor((stop+start)/2);
end
index = middle;
end