<P ><FONT face="Times New Roman">public static int binarySearch4(int[] a, int x, int n)</FONT></P>
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
<P ><FONT face="Times New Roman"> int middle = (left + right) / 2;</FONT></P>
<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
) return left;</FONT></P>
" B* K8 @/ _6 r3 A- |* E2 g+ a Y
<P ><FONT face="Times New Roman">}//if</FONT></P>
6 M L/ ]7 o2 X4 f) j# j2 }<P ><FONT face="Times New Roman">return –1;</FONT></P>
' u s+ ]- ^0 `. k<P ><FONT face="Times New Roman">}</FONT></P>
0 k, {# A( v4 [
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
( F4 F& ?( O# o k6 v<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
0 l4 A) _0 N" m2 L8 e<P ><FONT face="Times New Roman">public static int binarySearch5(int[] a, int x, int n)</FONT></P>
; n) i( D: c! V0 _. E! J! J; d<P ><FONT face="Times New Roman">{</FONT></P>
, X: K4 y: a& b5 b( l% j8 [/ p<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
: x% b2 E$ n" P" y<P ><FONT face="Times New Roman"> {</FONT></P>
6 F* F. ?# ~8 x, z
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
! ~8 v% d {- }7 a4 E# j ?% @
<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
- t: _) F+ p8 }* W<P ><FONT face="Times New Roman"> {</FONT></P>
$ |/ _4 B" C3 e5 H5 w* J( G1 X<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
, s) B- j4 y$ V# z
<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
; A/ W5 W/ s% ~$ ?0 }5 i* }5 t
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
+ D0 f2 b+ R5 m/ Q* l<P ><FONT face="Times New Roman">}//while</FONT></P>
$ f! n" S* [6 p( ?5 y P7 ^<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
9 }, T+ Y3 U0 P3 x. B( f<P ><FONT face="Times New Roman">}//if</FONT></P>
- K. @% ` P( |. A<P ><FONT face="Times New Roman">return –1;</FONT></P>
M Q$ Q$ h) { z( X<P ><FONT face="Times New Roman">}</FONT></P>
! g, O# K' ` V2 n2 M3 j
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
. _+ [: w( X" z1 y: } y
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
2 {; j. ^' _' i+ B' S. t) c& ^2 v<P ><FONT face="Times New Roman">public static int binarySearch6(int[] a, int x, int n)</FONT></P>
+ N6 b1 h2 k( V( o* w P4 u# B<P ><FONT face="Times New Roman">{</FONT></P>
$ Q5 D4 r/ Z2 ?% [<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
/ W) I# e, C3 @2 {
<P ><FONT face="Times New Roman"> {</FONT></P>
9 c) {. n0 @ C6 A<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
+ u# l! Y5 q& m8 ]) f: ~" O
<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
8 a$ W5 x; r. u2 Q+ G$ A
<P ><FONT face="Times New Roman"> {</FONT></P>
" M, Y( s, F, E' p. m: ?( e# Z<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
( c/ q5 B! j0 ~1 b2 w/ ]8 o! l
<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
! q( w/ j* J& C3 D8 O- a* k& w$ d<P ><FONT face="Times New Roman">else left = middle + 1;</FONT></P>
. Y. e1 o9 s6 o+ [5 v2 y
<P ><FONT face="Times New Roman">}//while</FONT></P>
' K8 V O0 q. I( q' x6 t ]& i, \<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
2 d2 P; E6 A b: O
<P ><FONT face="Times New Roman">}//if</FONT></P>
4 m" G3 e3 E4 V& V
<P ><FONT face="Times New Roman">return –1;</FONT></P>
v. Q$ y4 _% u7 N2 X
<P ><FONT face="Times New Roman">}</FONT></P>
& E6 @$ F3 E- B3 F3 L o
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
4 ?0 b. n9 ^( J
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
* Q4 F/ b. y6 b$ w<P ><FONT face="Times New Roman">public static int binarySearch7(int[] a, int x, int n)</FONT></P>
* c: H. {- {1 W2 H& t. P
<P ><FONT face="Times New Roman">{</FONT></P>
& B3 M5 v1 |' Y" E+ h& m
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
7 U* C$ s' P5 i) p% v9 x<P ><FONT face="Times New Roman"> {</FONT></P>
" |. r' P0 D& k1 E
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
1 j [( S% s* Y0 F<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
; A4 \& u9 P1 B. Q2 l" Q
<P ><FONT face="Times New Roman"> {</FONT></P>
. S1 O) ~' s* Q: |7 C
<P ><FONT face="Times New Roman"> int middle = (left + right +1) / 2;</FONT></P>
0 L% {: S* H) g8 r; p t: \$ `
<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle;</FONT></P>
D" N3 {& i( J! L7 r6 ^<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
5 t. q0 F4 G% Q9 F' ~8 j
<P ><FONT face="Times New Roman">}//while</FONT></P>
& {( d0 j4 Z1 j0 E2 A. ~
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
" ?5 T' z. @; r: O8 F
<P ><FONT face="Times New Roman">}//if</FONT></P>
, q5 C" K4 s# {9 d& q0 W3 Q<P ><FONT face="Times New Roman">return –1;</FONT></P>
) @% U2 v9 M$ p. p/ \2 A5 j" k
<P ><FONT face="Times New Roman">}</FONT></P>
5 z9 C: C, B7 r0 w3 u* x<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
9 l( ~; G0 }8 v6 \" ?/ e+ u- m
<P >解:(<FONT face="Times New Roman">1</FONT>)算法<FONT face="Times New Roman">1</FONT>不正确。<o:p></o:p></P>
3 B; o1 n' a) E; S' b$ o& P$ g$ |<P >当在数组<FONT face="Times New Roman">a</FONT>中找不到与<FONT face="Times New Roman">x</FONT>相等的元素时,算法将进入死循环状态。<o:p></o:p></P>
! a% I5 e" }; N! p5 @& R) q6 \
<P >原因:每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改不正确。应修改如下:<o:p></o:p></P>
3 }. i/ Q/ d) K0 k- g4 L
<P ><FONT face="Times New Roman">if(x > a[middle]) left = middle + 1;<o:p></o:p></FONT></P>
5 I8 L4 D7 A4 A& ]* `<P ><FONT face="Times New Roman"> else right = middle - 1;<o:p></o:p></FONT></P>
" Y% z" v+ e6 E& A4 G9 {
<P >(<FONT face="Times New Roman">2</FONT>)算法<FONT face="Times New Roman">2</FONT>不正确。<o:p></o:p></P>
# J. a" _8 y- \0 @, t; X' f2 G
<P >当<FONT face="Times New Roman">n</FONT>≥<FONT face="Times New Roman">2</FONT>时,如果条件<FONT face="Times New Roman">x = a[n-1] </FONT>且<FONT face="Times New Roman"> a[n-2] </FONT>≠<FONT face="Times New Roman"> a[n-1]</FONT>成立,则必将在某一步之后出现<FONT face="Times New Roman">x = a[left +1]</FONT>,导致永远不会出现<FONT face="Times New Roman">x = a[middle]</FONT>的情形,算法最终在<FONT face="Times New Roman">x = a
</FONT>时结束循环,导致错误地返回<FONT face="Times New Roman">-1</FONT>。<o:p></o:p></P>
" A$ P9 {: L f1 Z& B
<P >另外,当<FONT face="Times New Roman">n=0</FONT>时执行<FONT face="Times New Roman">if(x == a
)...</FONT>时将出现下标越界错误。<o:p></o:p></P>
: `. n9 u+ w8 s# n6 b) _<P >原因:循环结束条件错误,应改为<FONT face="Times New Roman">left <= right</FONT>。每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改也不正确。<o:p></o:p></P>
/ z0 P) M' M! ]" X
<P >(<FONT face="Times New Roman">3</FONT>)算法<FONT face="Times New Roman">3</FONT>不正确。<o:p></o:p></P>
) s* f: ]9 w9 g2 G<P >除了有与算法<FONT face="Times New Roman">2</FONT>相同的错误,另外当<FONT face="Times New Roman">n=0</FONT>或<FONT face="Times New Roman">n=1</FONT>时,必然进入死循环。<o:p></o:p></P>
4 z+ F- L. A! Y2 D1 q, l. }
<P >原因:与算法<FONT face="Times New Roman">2</FONT>相同。<o:p></o:p></P>
4 Z0 K: p$ J& H( c& n* B, l3 j
<P >(<FONT face="Times New Roman">4</FONT>)算法<FONT face="Times New Roman">4</FONT>不正确。<o:p></o:p></P>
4 N/ d7 U! K& P# ]! [7 N) X
<P >如果在循环过程中出现<FONT face="Times New Roman">left = right – 1</FONT>情况,算法即进入死循环。例如<FONT face="Times New Roman"> x</FONT>≥a[n-2]条件成立时,即必然进入死循环。<o:p></o:p></P>
8 X5 g0 Q* M7 K/ x
<P >原因:循环条件和对变量<FONT face="Times New Roman">left</FONT>值的修改有错误。<o:p></o:p></P>
; ^0 V( J. _, p5 m) x& _<P >(<FONT face="Times New Roman">5</FONT>)此算法正确。<o:p></o:p></P>
, f; L: o2 x6 Q: P! n7 T- w
<P >证明:当<FONT face="Times New Roman">n=0</FONT>或<FONT face="Times New Roman">n=1</FONT>时,算法显然正确。<o:p></o:p></P>
( U( |/ H2 U/ k9 {<P >当<FONT face="Times New Roman">n</FONT>≥<FONT face="Times New Roman">2</FONT>时,在循环结束前有<FONT face="Times New Roman">x</FONT>≥a[0]且left < right,<o:p></o:p></P>
! C9 h" H0 W% L. ^5 J" [<P >∴<FONT face="Times New Roman">middle = (left + right + 1) / 2 = [left + (right –1) + 1 +1] / 2 </FONT>≥ (2left + 2) / 2 = left + 1,<o:p></o:p></P>
# T5 H2 y. D8 n9 c) Y4 f8 R
<P >即:middle > left成立。<o:p></o:p></P>
, P3 O/ d5 C1 o0 r# y<P >且<FONT face="Times New Roman">middle = (left + right + 1) / 2 = [(left + 1) + right] / 2 </FONT>≤ 2right / 2 = right,<o:p></o:p></P>
4 Z3 X0 e6 r' r
<P >∴left < middle ≤ right恒成立。<o:p></o:p></P>
* T1 |! O6 Z* |& h( [
<P >因此,每次循环之后,right与left之差必然减小,在有限次循环后,必有left = right条件成立,从而循环结束。<o:p></o:p></P>
0 v" h( H5 n3 ]5 v9 U
<P >如果x值与数组a的某个元素值相等,则在循环结束时显然有x = a
且x = a
成立,否则x ≠a
,即未找到x,<o:p></o:p></P>
0 W |, S) e3 j
<P >∴返回结果正确。<o:p></o:p></P>
2 G$ T9 T; q. j- O<P >(6)算法6是错误的。<o:p></o:p></P>
# ]9 @) O7 y4 P& L<P >当执行到某次循环x = a[middle]成立时,再执行if 语句中的<o:p></o:p></P>
9 h" t9 p8 o/ P! ~3 Z9 t<P >left = middle + 1;<o:p></o:p></P>
) W6 ~9 e; B+ A0 Z<P >就把结果丢失了,导致错误。而且还可能会导致下标越界错误。例如:<o:p></o:p></P>
2 W2 |/ O- y% V/ h4 h, L+ j: H
<P >当n = 2且x = a[1]时即会出现这些情况。<o:p></o:p></P>
& p: t1 S A8 c) D4 d
<P >原因:if 语句中的left = middle + 1;应改为left = middle;<o:p></o:p></P>
' [! W% s/ k6 P* H5 x; A<P >(7)算法7是错误的。<o:p></o:p></P>
: I* y( t8 j3 @2 k+ J<P >在循环过程中,一旦出现<o:p></o:p></P>
- v9 o0 H* x1 o; ], j# B
<P >a
≤ x < a[left + 1],则必进入死循环。<o:p></o:p></P>7 F. |7 o1 r$ N3 z+ R+ a5 D1 K2 e
<P >原因:right值的修改不正确。<o:p></o:p></P>