Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
15
Task/Binary-search/Maple/binary-search-1.maple
Normal file
15
Task/Binary-search/Maple/binary-search-1.maple
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
BinarySearch := proc( A, value, low, high )
|
||||
description "recursive binary search";
|
||||
if high < low then
|
||||
FAIL
|
||||
else
|
||||
local mid := iquo( high + low, 2 );
|
||||
if A[ mid ] > value then
|
||||
thisproc( A, value, low, mid - 1 )
|
||||
elif A[ mid ] < value then
|
||||
thisproc( A, value, mid + 1, high )
|
||||
else
|
||||
mid
|
||||
end if
|
||||
end if
|
||||
end proc:
|
||||
17
Task/Binary-search/Maple/binary-search-2.maple
Normal file
17
Task/Binary-search/Maple/binary-search-2.maple
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
BinarySearch := proc( A, value )
|
||||
description "iterative binary search";
|
||||
local low, high;
|
||||
|
||||
low, high := ( lowerbound, upperbound )( A );
|
||||
while low <= high do
|
||||
local mid := iquo( low + high, 2 );
|
||||
if A[ mid ] > value then
|
||||
high := mid - 1
|
||||
elif A[ mid ] < value then
|
||||
low := mid + 1
|
||||
else
|
||||
return mid
|
||||
end if
|
||||
end do;
|
||||
FAIL
|
||||
end proc:
|
||||
17
Task/Binary-search/Maple/binary-search-3.maple
Normal file
17
Task/Binary-search/Maple/binary-search-3.maple
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
> N := 10:
|
||||
> P := [seq]( ithprime( i ), i = 1 .. N ):
|
||||
> BinarySearch( P, 12, 1, N ); # recursive version
|
||||
FAIL
|
||||
|
||||
> BinarySearch( P, 13, 1, N ); # recursive version
|
||||
6
|
||||
|
||||
> BinarySearch( Array( P ), 13, 1, N ); # make P into an array
|
||||
6
|
||||
|
||||
> PP := Array( -2 .. 7, P ): # check it works if the array is not 1-based.
|
||||
> BinarySearch( PP, 13 ); # iterative version
|
||||
3
|
||||
|
||||
> PP[ 3 ];
|
||||
13
|
||||
Loading…
Add table
Add a link
Reference in a new issue