Binsearch not updating
This algorithm does not determine if the element is actually found. The use of "exclusive" bounds simplifies the adjustment of the bounds: the appropriate bound simply receives the value of P, there is no 1 or - 1 adjustment at every step; similarly, the determination of an empty span is easy, and avoiding the risk of integer overflow via (L R)/2 is achieved at the same time.
(This can occur if the array size is greater than half the maximum integer value.) If signed integers are used, and overflows, it becomes a negative number, and dividing by 2 will still result in a negative number. It is because the method involves such a small amount of effort per iteration that minor changes offer a significant benefit.
It is also possible, when the element is not found, to return the "insertion point" for it (the index that the value would have if it were inserted into the array). There is no point in using an explicitly recursive version (even though the same actions may result during execution) because of the overhead of parameter passing and procedure entry/exit.
Recursive Pseudocode: The following algorithms return the leftmost place where the given element can be correctly inserted (and still maintain the sorted order). Later compilers offer features allowing the development of "generic" functions so that the same function name may be used yet the actual routine invoked will be selected according to how the parameters are integers or floating-point, and of different precisions.
To divide an unsigned number by 2, simply do a logical right shift. An assembler version of this routine attended to all these details.
* Binary search 05/03/2017BINSEAR CSECT USING BINSEAR, R13 base register B 72(R15) skip savearea DC 17F'0' savearea STM R14, R12,12(R13) save previous context ST R13,4(R15) link backward ST R15,8(R13) link forward LR R13, R15 set addressability MVC LOW,=H'1' low=1 MVC HIGH,=AL2((XVAL-T)/2) high=hbound(t) SR R6, R6 i=0 MVI F, X'00' f=false LH R4, LOW low DO WHILE=(CH, R4, LE, HIGH) do while lowxval then LH R2, MID mid BCTR R2,0 -1 STH R2, HIGH high=mid-1 ELSE , else LH R2, MID mid LA R2,1(R2) 1 STH R2, LOW low=mid 1 ENDIF , endif LH R4, LOW low ENDDO , enddo EXITDO EQU * exitdo: XDECO R6, XDEC edit i MVC PG(4), XDEC 8 output i MVC PG 4(6),=C' loops' XPRNT PG, L'PG print buffer LH R1, XVAL xval XDECO R1, XDEC edit xval MVC PG(4), XDEC 8 output xval IF CLI, F, EQ, X'01' THEN if f then MVC PG 4(10),=C' found at ' LH R1, MID mid XDECO R1, XDEC edit mid MVC PG 14(4), XDEC 8 output mid ELSE , else MVC PG 4(20),=C' is not in the list.' ENDIF , endif XPRNT PG, L'PG print buffer L R13,4(0, R13) restore previous savearea pointer LM R14, R12,12(R13) restore previous context XR R15, R15 rc=0 BR R14 exit T DC H'3', H'7', H'13', H'19', H'23', H'31', H'43', H'47' DC H'61', H'73', H'83', H'89', H'103', H'109', H'113', H'131' DC H'139', H'151', H'167', H'181', H'193', H'199', H'229', H'233' DC H'241', H'271', H'283', H'293', H'313', H'317', H'337', H'349'XVAL DC H'229' = 0 THEN PRINT "The value "; secret% " was found at index "; index% ELSE PRINT "The value "; secret% " was not found" ENDIF END REM Search ordered array A%() for the value S% from index B% to T% DEF FNwhere(A%(), S%, B%, T%) LOCAL H% H% = 2 WHILE H% @echo off & setlocal enabledelayedexpansion :: Binary Chop Algorithm - Michael Sanders 2017:::: example output...:::: binary chop algorithm vs. Some compilers do not produce machine code directly, but instead translate the source code into another language which is then compiled, and a common choice for that is C.