Java dichotomy
1 Public Int Found ( Int A [], Int R ){
2 Int Start, end, middle, RS;
3 Start = 0 ;
4 End = A. length;
5 RS = R;
6
7 While (Start < End ){
8
9 Middle = (Start + End) / 2 ;
10
11 If (RS = A [Middle]) {
12 Return Middle;
13 }
14 If (RS < A [Middle]) {
15 End = Middle - 1 ;
16
17 }
18 If (RS > A [Middle]) {
19 Start = Middle + 1 ;
20 }
21 }
22 Return - 1 ;
23
24 }
C # binary classification
Code
1 Public Int Found ( Int [], Int R)
2 {
3 Int Start, end, middle, RS;
4 Start = 0 ;
5 End = A. length;
6 RS = R;
7 While (Start < End)
8 {
9 Middle = (Start + End) / 2 ;
10 If (RS = A [Middle])
11 {
12 Return Middle;
13 }
14 If (RS < A [Middle])
15 {
16 End = Middle - 1 ;
17 }
18 If (RS > A [Middle])
19 {
20 Start = Middle + 1 ;
21 }
22 }
23 Return - 1 ;
24 }
VB. NET
Code
1 Public Function found (byval A as INTEGER (), byval R as integer) as integer
2 Dim start as integer, [end] As integer, middle as integer, RS as integer
3 Start = 0
4 [End] = A. Length
5 RS = R
6 While start < [End]
7 Middle = (Start + [End]) \ 2
8 If RS = A (middle) then
9 Return middle
10 End if
11 If RS < A (middle) then
12 [End] = Middle - 1
13 End if
14 If RS > A (middle) then
15 Start = Middle + 1
16 End if
17 End while
18 Return - 1
19 End Function