<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>
( p2 g! l6 V1 D9 @
<P ><FONT face="Times New Roman">}//if</FONT></P>
( D, f. ~3 k6 H<P ><FONT face="Times New Roman">return –1;</FONT></P>
) q" w$ {+ b0 V2 n- T0 G- b1 K# \<P ><FONT face="Times New Roman">}</FONT></P>
8 n- m; Q( p0 r
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
- P" D, R. S1 ?7 y0 {<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
& x; u2 S8 `1 |' ^' E! w0 N& O
<P ><FONT face="Times New Roman">public static int binarySearch5(int[] a, int x, int n)</FONT></P>
& B$ q1 N! V, p# E<P ><FONT face="Times New Roman">{</FONT></P>
# [; X1 s; t; ]
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
3 F* W. R" b) ?! n<P ><FONT face="Times New Roman"> {</FONT></P>
. j9 A) x3 A, `4 G. q6 [
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
' b% v/ e( `/ F0 t<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
* d/ W8 }! Y% w, c w5 m
<P ><FONT face="Times New Roman"> {</FONT></P>
. c. G' m; Q8 g<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
' E2 j- \5 _+ P* z2 Z( }8 T2 w<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
5 @8 ^' z$ n9 H* F; P: ^
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
* q2 x" E, s" T+ ]$ S, s<P ><FONT face="Times New Roman">}//while</FONT></P>
& m$ @: i& G/ m4 e2 H# |
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
+ w: H# x; l0 Y% u% f W
<P ><FONT face="Times New Roman">}//if</FONT></P>
! |; L1 e; Y6 o' }3 R! j% x<P ><FONT face="Times New Roman">return –1;</FONT></P>
/ }' ?5 a7 E* p+ m
<P ><FONT face="Times New Roman">}</FONT></P>
% }9 h& {* s5 q& d Q<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
' ^- [6 z; @) r4 ~ g2 {& t
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
- v9 F' U3 L- S7 B
<P ><FONT face="Times New Roman">public static int binarySearch6(int[] a, int x, int n)</FONT></P>
9 D0 P0 O/ g2 G1 @" j( I<P ><FONT face="Times New Roman">{</FONT></P>
! j" R3 y6 ~. [4 G) e2 V$ D: {
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
3 w' f- y, P8 |9 T9 C/ ~7 e% x
<P ><FONT face="Times New Roman"> {</FONT></P>
, Z( O# g+ p4 ^: L
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
- ]% n) v$ m8 v( q v6 x<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
$ Z. V% d' N- ^" B
<P ><FONT face="Times New Roman"> {</FONT></P>
?4 ]* C5 I( k+ Z; J5 ~
<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
+ K& i. a# x1 F, P
<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
# H6 u0 E& n6 p% `- \<P ><FONT face="Times New Roman">else left = middle + 1;</FONT></P>
$ [; [: ^8 ^8 K6 W+ m
<P ><FONT face="Times New Roman">}//while</FONT></P>
1 C8 m% E9 Q$ K% V% V
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
/ V! ^$ [$ D" t. ]
<P ><FONT face="Times New Roman">}//if</FONT></P>
* X7 h/ w% \/ m, p4 A<P ><FONT face="Times New Roman">return –1;</FONT></P>
. U4 E; s' }) F3 q<P ><FONT face="Times New Roman">}</FONT></P>
; z7 Z# n/ Z$ h<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
3 m0 B; R4 f! \ j7 \7 z1 a<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
9 T ^6 G% r0 i6 E" L% p<P ><FONT face="Times New Roman">public static int binarySearch7(int[] a, int x, int n)</FONT></P>
, L2 [; J9 p5 N" ^' g# n0 Y
<P ><FONT face="Times New Roman">{</FONT></P>
) \0 l0 H* c, w$ S" A
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
! _5 [. t& A3 G8 i5 Q- B
<P ><FONT face="Times New Roman"> {</FONT></P>
9 _' z5 M' l7 F2 } M& B
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
8 m9 z$ `# W/ k4 }- P% C<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
) D+ k* L6 ?+ _+ r
<P ><FONT face="Times New Roman"> {</FONT></P>
5 c! V6 @3 o1 c4 o0 S8 l2 v2 ]
<P ><FONT face="Times New Roman"> int middle = (left + right +1) / 2;</FONT></P>
5 p( w+ G) K5 S# h* r
<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle;</FONT></P>
' u e g1 w" i N7 }5 _<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
) q0 J, S! ~# R8 ^0 E+ e" h9 u: q) {, }<P ><FONT face="Times New Roman">}//while</FONT></P>
' W- d% D. h0 L; j<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
7 n9 ^9 f. Y- V* {9 s
<P ><FONT face="Times New Roman">}//if</FONT></P>
+ T4 v U3 x3 G4 p
<P ><FONT face="Times New Roman">return –1;</FONT></P>
' G1 {# v6 w8 b+ [9 ?
<P ><FONT face="Times New Roman">}</FONT></P>
$ w% `' K2 n- k% H- ~3 V* D0 J- X<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
3 w# Y7 e: f( T; U5 e3 |! @<P >解:(<FONT face="Times New Roman">1</FONT>)算法<FONT face="Times New Roman">1</FONT>不正确。<o:p></o:p></P>
- O2 [# [" `, v* P, p9 Y8 a
<P >当在数组<FONT face="Times New Roman">a</FONT>中找不到与<FONT face="Times New Roman">x</FONT>相等的元素时,算法将进入死循环状态。<o:p></o:p></P>
* \9 c& S2 ^7 t9 `$ A4 [<P >原因:每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改不正确。应修改如下:<o:p></o:p></P>
3 @* M/ r1 ]) J6 m6 X7 W. y
<P ><FONT face="Times New Roman">if(x > a[middle]) left = middle + 1;<o:p></o:p></FONT></P>
- w% A' d3 m7 d+ T- x3 o<P ><FONT face="Times New Roman"> else right = middle - 1;<o:p></o:p></FONT></P>
2 N0 e4 o! _2 v% T% `
<P >(<FONT face="Times New Roman">2</FONT>)算法<FONT face="Times New Roman">2</FONT>不正确。<o:p></o:p></P>
' Q c1 V9 D; ?1 F9 ^<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>
2 ~. z o. U2 ~1 q& K; q, k9 f<P >另外,当<FONT face="Times New Roman">n=0</FONT>时执行<FONT face="Times New Roman">if(x == a
)...</FONT>时将出现下标越界错误。<o:p></o:p></P>
9 X5 K( r o( x' K; D5 a& Q4 n% W<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>
) f/ {+ G- B* H5 T5 u) @0 @ ?
<P >(<FONT face="Times New Roman">3</FONT>)算法<FONT face="Times New Roman">3</FONT>不正确。<o:p></o:p></P>
. U% }8 a1 h" a) F( m
<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>
9 I- k4 B$ g# l# N2 z+ o9 V: V! E<P >原因:与算法<FONT face="Times New Roman">2</FONT>相同。<o:p></o:p></P>
- g. Q, q/ q4 A1 ?1 I3 [' B<P >(<FONT face="Times New Roman">4</FONT>)算法<FONT face="Times New Roman">4</FONT>不正确。<o:p></o:p></P>
" T4 [5 C8 ~/ Q: g+ |6 U3 U& A<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>
! ~ K/ ?8 ^1 k3 I. J- s<P >原因:循环条件和对变量<FONT face="Times New Roman">left</FONT>值的修改有错误。<o:p></o:p></P>
' z( w) t0 a) G. ?8 H
<P >(<FONT face="Times New Roman">5</FONT>)此算法正确。<o:p></o:p></P>
. C2 s8 t8 Y& H
<P >证明:当<FONT face="Times New Roman">n=0</FONT>或<FONT face="Times New Roman">n=1</FONT>时,算法显然正确。<o:p></o:p></P>
, t4 s9 v# j9 ]5 W<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>
0 @8 M0 A! q+ d$ t! b<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>
8 ^8 z% e3 D6 [' T<P >即:middle > left成立。<o:p></o:p></P>
4 j I+ o, |" u; @<P >且<FONT face="Times New Roman">middle = (left + right + 1) / 2 = [(left + 1) + right] / 2 </FONT>≤ 2right / 2 = right,<o:p></o:p></P>
7 n3 l( k5 o1 H# [7 ^
<P >∴left < middle ≤ right恒成立。<o:p></o:p></P>
" z6 N1 ~& T6 K4 }, z$ d- ^& ?2 L
<P >因此,每次循环之后,right与left之差必然减小,在有限次循环后,必有left = right条件成立,从而循环结束。<o:p></o:p></P>
/ o9 ^0 d7 n6 K" _2 ~& f, c<P >如果x值与数组a的某个元素值相等,则在循环结束时显然有x = a
且x = a
成立,否则x ≠a
,即未找到x,<o:p></o:p></P>
1 A# T: u9 ]) t
<P >∴返回结果正确。<o:p></o:p></P>
8 i( |& l6 Q# G+ i+ T" Y0 t
<P >(6)算法6是错误的。<o:p></o:p></P>
7 k1 x4 g8 o7 L" Q<P >当执行到某次循环x = a[middle]成立时,再执行if 语句中的<o:p></o:p></P>
8 ~* R2 S- t& n3 R
<P >left = middle + 1;<o:p></o:p></P>
7 |2 k, [' B* @# u, q& o# b<P >就把结果丢失了,导致错误。而且还可能会导致下标越界错误。例如:<o:p></o:p></P>
! J1 s, n; T+ N
<P >当n = 2且x = a[1]时即会出现这些情况。<o:p></o:p></P>
" t k& d! V% } F% d% }<P >原因:if 语句中的left = middle + 1;应改为left = middle;<o:p></o:p></P>
% Z$ {1 U# m2 K+ a" B7 A
<P >(7)算法7是错误的。<o:p></o:p></P>
+ T: h; g, \' J4 Y5 ~
<P >在循环过程中,一旦出现<o:p></o:p></P>
3 H& `1 z! a/ m! T9 C/ N
<P >a
≤ x < a[left + 1],则必进入死循环。<o:p></o:p></P>1 S% h! o$ @2 h" |# O7 X; t6 |
<P >原因:right值的修改不正确。<o:p></o:p></P>