Matlab İkili Arama(Binary Search)

İkili arama algoritmsını anlatmıyacağım internette görselleriyle birlikte çok fazla kaynak var.
function index = binary_search_recursive ...
(vector,target,first,last)
mid = fix( (first + last)/2 );
if ~(first <= last) % If first and last out of order..
index = -1; % ..then target not on the list!
elseif target == vector(mid)
index = mid; % found it!
elseif target < vector(mid)
index = binary_search_recursive ...
(vector,target,first, mid-1);
else
index = binary_search_recursive ...
(vector,target,mid+1, last);
end

Yorumlar

Popüler Yayınlar