Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
|
|
@ -30,20 +30,19 @@ PROC recursive binary search = ([]ELEMENT hay stack, ELEMENT needle)INT: (
|
|||
ELSE mid FI
|
||||
FI
|
||||
);
|
||||
# Test cases: #
|
||||
test:(
|
||||
ELEMENT needle = "mister";
|
||||
[]ELEMENT hay stack = ("AA","Maestro","Mario","Master","Mattress","Mister","Mistress","ZZ"),
|
||||
test cases = ("A","Master","Monk","ZZZ");
|
||||
|
||||
PROC test search = (PROC([]ELEMENT, ELEMENT)INT search, []ELEMENT test cases)VOID:
|
||||
FOR case TO UPB test cases DO
|
||||
PROC test search = (PROC([]ELEMENT, ELEMENT)INT search, []ELEMENT hay stack, []ELEMENT test cases)VOID:
|
||||
FOR case TO UPB test cases DO
|
||||
ELEMENT needle = test cases[case];
|
||||
INT index = search(hay stack, needle);
|
||||
BOOL found = ( index <= 0 | FALSE | hay stack[index]=needle);
|
||||
print(("""", needle, """ ", (found|"FOUND at"|"near"), " index ", whole(index, 0), newline))
|
||||
OD;
|
||||
test search(iterative binary search, test cases);
|
||||
test search(recursive binary search, test cases)
|
||||
)
|
||||
OD;
|
||||
|
||||
BEGIN # Test cases: #
|
||||
[]ELEMENT hay stack = ("AA","Maestro","Mario","Master","Mattress","Mister","Mistress","ZZ")
|
||||
, test cases = ("A","Master","Monk","ZZZ")
|
||||
;
|
||||
test search(iterative binary search, hay stack, test cases);
|
||||
test search(recursive binary search, hay stack, test cases)
|
||||
END
|
||||
END
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
type BinarySearch:Recursive
|
||||
fun binarySearch = int by List values, int value
|
||||
fun recurse = int by int low, int high
|
||||
fun binarySearch ← int by List values, int value
|
||||
fun recurse ← int by int low, int high
|
||||
if high < low do return -1 end
|
||||
int mid = low + (high - low) / 2
|
||||
int mid ← low + (high - low) / 2
|
||||
return when(values[mid] > value,
|
||||
recurse(low, mid - 1),
|
||||
when(values[mid] < value,
|
||||
|
|
@ -12,23 +12,22 @@ fun binarySearch = int by List values, int value
|
|||
return recurse(0, values.length - 1)
|
||||
end
|
||||
type BinarySearch:Iterative
|
||||
fun binarySearch = int by List values, int value
|
||||
int low = 0
|
||||
int high = values.length - 1
|
||||
while low <= high
|
||||
int mid = low + (high - low) / 2
|
||||
if values[mid] > value do high = mid - 1
|
||||
else if values[mid] < value do low = mid + 1
|
||||
fun binarySearch ← int by List values, int value
|
||||
int low ← 0
|
||||
int high ← values.length - 1
|
||||
while low ≤ high
|
||||
int mid ← low + (high - low) / 2
|
||||
if values[mid] > value do high ← mid - 1
|
||||
else if values[mid] < value do low ← mid + 1
|
||||
else do return mid
|
||||
end
|
||||
end
|
||||
return -1
|
||||
end
|
||||
List values = int[0, 1, 4, 5, 6, 7, 8, 9, 12, 26, 45, 67, 78,
|
||||
List values ← int[0, 1, 4, 5, 6, 7, 8, 9, 12, 26, 45, 67, 78,
|
||||
90, 98, 123, 211, 234, 456, 769, 865, 2345, 3215, 14345, 24324]
|
||||
List matches = int[24324, 32, 78, 287, 0, 42, 45, 99999]
|
||||
for each int match in matches
|
||||
writeLine("index is: " +
|
||||
BinarySearch:Recursive.binarySearch(values, match) + ", " +
|
||||
BinarySearch:Iterative.binarySearch(values, match))
|
||||
end
|
||||
List matches ← int[24324, 32, 78, 287, 0, 42, 45, 99999]
|
||||
writeLine("recursive | iterative")
|
||||
matches.list(<int match|writeLine(
|
||||
(text!BinarySearch:Recursive.binarySearch(values, match)).padStart(9, " "), " | ",
|
||||
(text!BinarySearch:Iterative.binarySearch(values, match)).padStart(9, " ")))
|
||||
|
|
|
|||
|
|
@ -1,13 +1,11 @@
|
|||
'X Y L H M'=. i.5 NB. Setting mnemonics for boxes
|
||||
f=. &({::) NB. Fetching the contents of a box
|
||||
o=. @: NB. Composing verbs (functions)
|
||||
'`X Y L H M'=. ,{{y&{::`''}}&>i.5 NB. Setting mnemonics for boxes (e.g. X=.0&{::)
|
||||
'l h m' =. 2 3 4 NB. more box mnemonics (used for e.g. m})
|
||||
|
||||
boxes=. ; , a: $~ 3: NB. Appending 3 (empty) boxes to the inputs
|
||||
LowHigh=. (0 ; # o (X f)) (L,H)} ] NB. Setting the low and high bounds
|
||||
midpoint=. < o (<. o (2 %~ L f + H f)) M} ] NB. Updating the midpoint
|
||||
case=. >: o * o (Y f - M f { X f) NB. Less=0, equal=1, or greater=2
|
||||
boxes =. ;,a:$~3: NB. Appending 3 (empty) boxes to the inputs
|
||||
LowHigh =. (0;#@X) (l,h)} ] NB. Setting the low and high bounds
|
||||
midpoint=. <@(<.@(2%~L+H)) m} ] NB. Updating the midpoint
|
||||
case =. >:@:*@(Y-M{X) NB. Less=0, equal=1, or greater=2
|
||||
|
||||
squeeze=. (< o (_1 + M f) H} ])`(< o _: L} ])`(< o (1 + M f) L} ])@.case
|
||||
return=. (M f) o ((<@:('Not Found'"_) M} ]) ^: (_ ~: L f))
|
||||
|
||||
bs=. return o (squeeze o midpoint ^: (L f <: H f) ^:_) o LowHigh o boxes
|
||||
squeeze =. (<@(_1+M) h} ])`(<@_ l} ])`(<@(1+M) l} ])@.case
|
||||
return =. [: M (<@'Not Found' m} ])^:(_~:L)
|
||||
bs =. return@(squeeze@midpoint^:(L<:H)^:_)@LowHigh@boxes
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
'X Y L H M'=. i.5 NB. Setting mnemonics for boxes
|
||||
f=. &({::) NB. Fetching the contents of a box
|
||||
o=. @: NB. Composing verbs (functions)
|
||||
'`X Y L H M'=. ,{{y&{::`''}}&>i.5 NB. Setting mnemonics for boxes (e.g. X=.0&{::)
|
||||
'l h m' =. 2 3 4 NB. more box mnemonics (used for e.g. m})
|
||||
|
||||
boxes=. a: ,~ ; NB. Appending 1 (empty) box to the inputs
|
||||
midpoint=. < o (<. o (2 %~ L f + H f)) M} ] NB. Updating the midpoint
|
||||
case=. >: o * o (Y f - M f { X f) NB. Less=0, equal=1, or greater=2
|
||||
boxes =. a:,~; NB. Appending 3 (empty) boxes to the inputs
|
||||
LowHigh =. (0;#@X) (l,h)} ] NB. Setting the low and high bounds
|
||||
midpoint=. <@(<.@(2%~L+H)) m} ] NB. Updating the midpoint
|
||||
case =. >:@:*@(Y-M{X) NB. Less=0, equal=1, or greater=2
|
||||
|
||||
recur=. (X f bs Y f ; L f ; (_1 + M f))`(M f)`(X f bs Y f ; (1 + M f) ; H f)@.case
|
||||
|
||||
bs=. (recur o midpoint`('Not Found'"_) @. (H f < L f) o boxes) :: ([ bs ] ; 0 ; (<: o # o [))
|
||||
recur =. (X bs Y;L;(_1+M))`M`(X bs Y;(1+M);H)@.case
|
||||
bs =. recur@midpoint`('Not Found'"_)@.(H<L)@boxes :: ([ bs ]; 0; <:@#@[)
|
||||
|
|
|
|||
3
Task/Binary-search/PascalABC.NET/binary-search-2.pas
Normal file
3
Task/Binary-search/PascalABC.NET/binary-search-2.pas
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
##
|
||||
var s := |2,3,4,5,6,7,8,9,10,12,14,16,18,20,22,25,27,30|;
|
||||
s.binarysearch(10).println;
|
||||
Loading…
Add table
Add a link
Reference in a new issue