<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"> while(left < right)</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>
7 {1 X* v& O% _$ B6 Y ]
<P ><FONT face="Times New Roman">}//if</FONT></P>
: a; u" _" P- ^3 P1 g0 J: E& I' l
<P ><FONT face="Times New Roman">return –1;</FONT></P>
' \+ X( J9 C2 G( k1 z: ?3 I
<P ><FONT face="Times New Roman">}</FONT></P>
( K! P `- O5 _' _* s- d<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
* h& ?" Q$ L. V; A W8 G5 V<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
) y9 ] v& I' |5 L7 n9 e# Z' j8 H
<P ><FONT face="Times New Roman">public static int binarySearch5(int[] a, int x, int n)</FONT></P>
, Y0 [" V( c2 u# L8 e; ^<P ><FONT face="Times New Roman">{</FONT></P>
! B1 M: n8 [3 E! @<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
; S, P/ N' I3 `! s% @<P ><FONT face="Times New Roman"> {</FONT></P>
. g! z: x& k8 Q! e9 u<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
& S& |4 \/ ?0 ? D1 ~9 x3 {' u<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
/ [& f' i: t0 X; b d
<P ><FONT face="Times New Roman"> {</FONT></P>
& q, a, U, u% |9 l
<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
b$ m; D- ]# ]" g" F<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
) ]( U/ i1 r9 i& j" ?
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
9 u! } k& }! _<P ><FONT face="Times New Roman">}//while</FONT></P>
; u& J4 j: ?7 O: f& {+ f
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
y) H& N$ W: [# S8 I L' f8 w! R( Z<P ><FONT face="Times New Roman">}//if</FONT></P>
, S' o, D' c! o1 [<P ><FONT face="Times New Roman">return –1;</FONT></P>
- o7 X0 ]! w9 U, x1 ]
<P ><FONT face="Times New Roman">}</FONT></P>
- F5 ]: p7 _* [ O* V3 v
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
, J1 W1 `5 N% T
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
* N6 B4 `5 \* j1 |/ p" v
<P ><FONT face="Times New Roman">public static int binarySearch6(int[] a, int x, int n)</FONT></P>
2 p" [& R( ]4 l1 w ?<P ><FONT face="Times New Roman">{</FONT></P>
/ O' C5 B# D7 ?<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
" k; K+ y `5 n- K9 o& Q
<P ><FONT face="Times New Roman"> {</FONT></P>
, Q/ E" Y: L$ }3 l! y7 y% T- m
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
4 R) n6 l5 f, V- f( y
<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
G1 ]2 a9 I: k' K! r<P ><FONT face="Times New Roman"> {</FONT></P>
" _0 A3 q7 x$ T: q( _9 F
<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
% N% E. s! p" f9 s' R5 R/ p<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
8 @5 h B! W! X: q/ U
<P ><FONT face="Times New Roman">else left = middle + 1;</FONT></P>
; G- G) d0 R; p8 B" |* ]) C1 m<P ><FONT face="Times New Roman">}//while</FONT></P>
8 x! v: b0 X* M# Y/ g2 i$ S( A+ R# t
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
" w3 C$ N u$ r" U; X3 @
<P ><FONT face="Times New Roman">}//if</FONT></P>
0 n3 R& c9 X) i) A* ^
<P ><FONT face="Times New Roman">return –1;</FONT></P>
0 a# R& A0 g# d3 E8 z# d<P ><FONT face="Times New Roman">}</FONT></P>
) ^& u: p9 w3 Q% x! h I
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
8 w! P5 C) P# G$ i/ W
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
9 d( z& U" p1 n2 J( Q
<P ><FONT face="Times New Roman">public static int binarySearch7(int[] a, int x, int n)</FONT></P>
6 t. T& j3 [3 O p+ D! _
<P ><FONT face="Times New Roman">{</FONT></P>
5 L8 D* y( C* o<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
( k6 O8 c) R+ E A! [<P ><FONT face="Times New Roman"> {</FONT></P>
) g* s1 Y$ V7 U0 M! c) s4 n5 c<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
( X Z( o( Z c3 _0 |6 ?! u
<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
2 H7 c8 v% W$ C' Y& k3 l; L5 M
<P ><FONT face="Times New Roman"> {</FONT></P>
1 `1 w/ f- L1 J( T& g5 J! ~7 i! |# Q<P ><FONT face="Times New Roman"> int middle = (left + right +1) / 2;</FONT></P>
$ O1 e, {2 O/ k, g3 d1 G
<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle;</FONT></P>
. e& L) V# Y- e6 l* W- e! C
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
8 Y3 Q( a1 v) B A0 y6 w: a$ ^<P ><FONT face="Times New Roman">}//while</FONT></P>
* T; W5 M8 f/ J+ c" {& \<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
* D6 H0 Z! g) F) v+ ]: c% G<P ><FONT face="Times New Roman">}//if</FONT></P>
X4 [% W/ C) z7 J0 y
<P ><FONT face="Times New Roman">return –1;</FONT></P>
9 L$ u6 j/ o. d& g1 O<P ><FONT face="Times New Roman">}</FONT></P>
" H2 A. Z, I2 q N/ e( E<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
6 K& Z) t( \2 q4 W
<P >解:(<FONT face="Times New Roman">1</FONT>)算法<FONT face="Times New Roman">1</FONT>不正确。<o:p></o:p></P>
9 I7 {! I! g8 R* n% U<P >当在数组<FONT face="Times New Roman">a</FONT>中找不到与<FONT face="Times New Roman">x</FONT>相等的元素时,算法将进入死循环状态。<o:p></o:p></P>
% `- u( k) f" C0 c0 w A H
<P >原因:每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改不正确。应修改如下:<o:p></o:p></P>
1 a( ?9 ?2 {1 D; W) Q
<P ><FONT face="Times New Roman">if(x > a[middle]) left = middle + 1;<o:p></o:p></FONT></P>
# w( c$ s1 t% {7 J+ P+ G. x! D, l<P ><FONT face="Times New Roman"> else right = middle - 1;<o:p></o:p></FONT></P>
. v7 J9 I1 Q/ t" [/ R3 `
<P >(<FONT face="Times New Roman">2</FONT>)算法<FONT face="Times New Roman">2</FONT>不正确。<o:p></o:p></P>
; Z! l7 V8 Y1 Z+ I7 p9 x5 A
<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>
" H# R. `+ g( _0 _5 P4 |<P >另外,当<FONT face="Times New Roman">n=0</FONT>时执行<FONT face="Times New Roman">if(x == a
)...</FONT>时将出现下标越界错误。<o:p></o:p></P>
1 h+ m. w( u! x: U<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>
2 Z( \8 K: h3 ]7 u$ s<P >(<FONT face="Times New Roman">3</FONT>)算法<FONT face="Times New Roman">3</FONT>不正确。<o:p></o:p></P>
7 V0 y$ o* R! q<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>
/ }! l- \# r: k9 D, d0 Q4 Z<P >原因:与算法<FONT face="Times New Roman">2</FONT>相同。<o:p></o:p></P>
5 _1 K8 Y6 M5 R- S/ Z
<P >(<FONT face="Times New Roman">4</FONT>)算法<FONT face="Times New Roman">4</FONT>不正确。<o:p></o:p></P>
! i0 t1 v3 |8 P1 o<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>
) P+ D- B) d5 G/ M) {, [
<P >原因:循环条件和对变量<FONT face="Times New Roman">left</FONT>值的修改有错误。<o:p></o:p></P>
, J |8 r) `% q5 H. ?
<P >(<FONT face="Times New Roman">5</FONT>)此算法正确。<o:p></o:p></P>
' i; O0 ^2 j0 Y( n% K7 O3 C<P >证明:当<FONT face="Times New Roman">n=0</FONT>或<FONT face="Times New Roman">n=1</FONT>时,算法显然正确。<o:p></o:p></P>
# V1 G1 N' N0 m6 w) L1 F
<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>
. u" m9 m. P# z9 S7 _
<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>
) ?' S% Y7 f3 T- K/ G<P >即:middle > left成立。<o:p></o:p></P>
+ b7 V' y7 U: B4 J3 M9 X
<P >且<FONT face="Times New Roman">middle = (left + right + 1) / 2 = [(left + 1) + right] / 2 </FONT>≤ 2right / 2 = right,<o:p></o:p></P>
1 W U# g0 U3 B) }! b; e" h) \6 ]
<P >∴left < middle ≤ right恒成立。<o:p></o:p></P>
+ K- q |' z! O
<P >因此,每次循环之后,right与left之差必然减小,在有限次循环后,必有left = right条件成立,从而循环结束。<o:p></o:p></P>
+ C p6 B5 ?) g" |8 a+ ^$ k3 L/ {<P >如果x值与数组a的某个元素值相等,则在循环结束时显然有x = a
且x = a
成立,否则x ≠a
,即未找到x,<o:p></o:p></P>
8 Q9 s& @3 x% x2 u& c
<P >∴返回结果正确。<o:p></o:p></P>
. R& }: u8 Y! C5 I' h4 L4 R" u6 L
<P >(6)算法6是错误的。<o:p></o:p></P>
9 a3 g1 y- [) w7 S" _1 V
<P >当执行到某次循环x = a[middle]成立时,再执行if 语句中的<o:p></o:p></P>
- N' t S4 y2 o: ^<P >left = middle + 1;<o:p></o:p></P>
0 X4 P8 f( D( o% t7 C! i6 {<P >就把结果丢失了,导致错误。而且还可能会导致下标越界错误。例如:<o:p></o:p></P>
; [* b: {2 E; R/ k7 Z L. K
<P >当n = 2且x = a[1]时即会出现这些情况。<o:p></o:p></P>
& V3 K, m3 h0 f
<P >原因:if 语句中的left = middle + 1;应改为left = middle;<o:p></o:p></P>
7 m9 i7 ?6 r; L: T& P<P >(7)算法7是错误的。<o:p></o:p></P>
9 d* f/ S _: H$ f9 c" @<P >在循环过程中,一旦出现<o:p></o:p></P>
4 Z( @- l8 I8 O4 x<P >a
≤ x < a[left + 1],则必进入死循环。<o:p></o:p></P>
! H# Z/ e: X$ P# {/ Z C, u; M<P >原因:right值的修改不正确。<o:p></o:p></P>