This commit is contained in:
Ingy döt Net 2013-04-10 15:42:53 -07:00
parent 051504d65b
commit 0457928c3e
295 changed files with 3588 additions and 3 deletions

View file

@ -0,0 +1,20 @@
function mid = binarySearchRec(list,value,low,high)
if( high < low )
mid = [];
return
end
mid = floor((low + high)/2);
if( list(mid) > value )
mid = binarySearchRec(list,value,low,mid-1);
return
elseif( list(mid) < value )
mid = binarySearchRec(list,value,mid+1,high);
return
else
return
end
end

View file

@ -0,0 +1,5 @@
>> binarySearchRec([1 2 3 4 5 6 6.5 7 8 9 11 18],6.5,1,numel([1 2 3 4 5 6 6.5 7 8 9 11 18]))
ans =
7

View file

@ -0,0 +1,20 @@
function mid = binarySearchIter(list,value)
low = 0;
high = numel(list) - 1;
while( low <= high )
mid = floor((low + high)/2);
if( list(mid) > value )
high = mid - 1;
elseif( list(mid) < value )
low = mid + 1;
else
return
end
end
mid = [];
end

View file

@ -0,0 +1,5 @@
>> binarySearchIter([1 2 3 4 5 6 6.5 7 8 9 11 18],6.5)
ans =
7