<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>
2 p' p s+ N$ x& w<P ><FONT face="Times New Roman">}//if</FONT></P>
+ a; D. |, S0 G* L, u5 b) g
<P ><FONT face="Times New Roman">return –1;</FONT></P>
. d* a& A {9 \; K2 b2 ^* I4 q- g
<P ><FONT face="Times New Roman">}</FONT></P>
1 B. i! q! e7 e* L' m' Q
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
7 q! |- k8 x; D<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
4 x1 }% X9 T# ]8 n R
<P ><FONT face="Times New Roman">public static int binarySearch5(int[] a, int x, int n)</FONT></P>
% ]7 t: M6 v8 e<P ><FONT face="Times New Roman">{</FONT></P>
# p4 E/ \3 n; S; @# h& T4 K7 g<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
_0 P! u) Y7 J6 q7 U
<P ><FONT face="Times New Roman"> {</FONT></P>
5 p* M9 x y: }* M, M* r<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
) H3 `3 v2 X: _* r; `% z
<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
% J$ d* Q! V$ o3 [& x! [<P ><FONT face="Times New Roman"> {</FONT></P>
6 R ^* ?% y' D0 U/ p7 _. D& f! ]<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
, l. P& O0 c% }3 `! ?' ~
<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
9 d; q$ t0 Y$ m9 M3 @# b1 @
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
6 u) _$ d# R9 f" W7 m<P ><FONT face="Times New Roman">}//while</FONT></P>
; j9 f9 E, L; I# M8 i$ |<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
/ g3 y" S- q0 s: ?8 L2 g2 c$ {
<P ><FONT face="Times New Roman">}//if</FONT></P>
6 p4 T* Y4 t( ~. @9 s8 `
<P ><FONT face="Times New Roman">return –1;</FONT></P>
5 T8 M" \" S8 R' @$ }<P ><FONT face="Times New Roman">}</FONT></P>
0 z+ @1 ?% v7 \/ q6 Q% e0 | }
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
8 `! @( v4 q/ I+ y& X! d
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
4 f1 I i) q- _7 [: V
<P ><FONT face="Times New Roman">public static int binarySearch6(int[] a, int x, int n)</FONT></P>
6 b/ P/ H' x \8 F
<P ><FONT face="Times New Roman">{</FONT></P>
: Q8 |( o: f; H2 Z) |2 L<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
8 N' h; Y3 K; k<P ><FONT face="Times New Roman"> {</FONT></P>
( q2 R$ V% J: w3 g" S, h
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
8 D; z8 v' p5 N, B6 s, y<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
1 `$ _" v+ [- O' B: ^* W
<P ><FONT face="Times New Roman"> {</FONT></P>
- Y6 c4 x2 j5 w3 n2 n' j# g<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
9 Y. @6 F: v' e: P9 n0 ]& @<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
X% m# `& a/ }7 r( w$ l
<P ><FONT face="Times New Roman">else left = middle + 1;</FONT></P>
- r0 m1 C$ m) F( n1 T" N<P ><FONT face="Times New Roman">}//while</FONT></P>
/ y. q2 B! i: a) M
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
& J5 P# o( s0 S<P ><FONT face="Times New Roman">}//if</FONT></P>
6 O) G: z6 X! ?<P ><FONT face="Times New Roman">return –1;</FONT></P>
0 D; X' m4 x: p# ?3 V
<P ><FONT face="Times New Roman">}</FONT></P>
4 Q+ I$ Y0 @% q' X* _8 ]9 o
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
3 X& Z! a! o: X& E- n& S<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
- w* _+ H; I* T2 u
<P ><FONT face="Times New Roman">public static int binarySearch7(int[] a, int x, int n)</FONT></P>
: A. ~8 ~0 O& ]! I
<P ><FONT face="Times New Roman">{</FONT></P>
. @! U' P% w h% x
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
8 v# X+ g9 }3 l# }/ ]" f
<P ><FONT face="Times New Roman"> {</FONT></P>
. X3 m% y) R: C! [ o
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
+ V' j( p ~8 P% [<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
" }0 I7 K3 j5 \& n' F
<P ><FONT face="Times New Roman"> {</FONT></P>
6 L# F3 K" l) q; h<P ><FONT face="Times New Roman"> int middle = (left + right +1) / 2;</FONT></P>
/ J5 F5 q; Y: ?5 u<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle;</FONT></P>
( B% h/ R* _9 `
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
' E1 j6 R, m( [% _$ W3 r<P ><FONT face="Times New Roman">}//while</FONT></P>
3 x6 f* ~- v/ w& F' z8 x+ |' k2 i<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
8 \8 e8 @* N0 t9 I<P ><FONT face="Times New Roman">}//if</FONT></P>
5 ^& ~" p2 L( D/ r<P ><FONT face="Times New Roman">return –1;</FONT></P>
' X% y9 D( a$ H<P ><FONT face="Times New Roman">}</FONT></P>
; U2 v3 Z# k. |1 T: t7 T
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
* X7 ~$ I2 L( H3 |" [# H8 d
<P >解:(<FONT face="Times New Roman">1</FONT>)算法<FONT face="Times New Roman">1</FONT>不正确。<o:p></o:p></P>
0 P9 Q! E) y9 x; o, b
<P >当在数组<FONT face="Times New Roman">a</FONT>中找不到与<FONT face="Times New Roman">x</FONT>相等的元素时,算法将进入死循环状态。<o:p></o:p></P>
+ [8 l9 h U. {/ F1 |: t
<P >原因:每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改不正确。应修改如下:<o:p></o:p></P>
& ?% O: D: s0 S
<P ><FONT face="Times New Roman">if(x > a[middle]) left = middle + 1;<o:p></o:p></FONT></P>
4 `- d" ?: B1 Z7 @, k& Q/ m<P ><FONT face="Times New Roman"> else right = middle - 1;<o:p></o:p></FONT></P>
8 z4 O% x" @1 t) v ?8 H<P >(<FONT face="Times New Roman">2</FONT>)算法<FONT face="Times New Roman">2</FONT>不正确。<o:p></o:p></P>
2 {. n! [) B; E( p0 L1 S8 P<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>
0 n: s, j; Y+ O/ r' j5 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 a0 g4 o5 N M! }5 N2 v<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>
# M7 A6 \6 r# A2 _5 r7 I. {
<P >(<FONT face="Times New Roman">3</FONT>)算法<FONT face="Times New Roman">3</FONT>不正确。<o:p></o:p></P>
" g7 B$ D1 }! `: A$ [<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 o( t3 z: e5 v2 X M
<P >原因:与算法<FONT face="Times New Roman">2</FONT>相同。<o:p></o:p></P>
5 B# K$ {' u2 a' g; I7 E: T<P >(<FONT face="Times New Roman">4</FONT>)算法<FONT face="Times New Roman">4</FONT>不正确。<o:p></o:p></P>
% D; R7 s9 ~- ]( E* M' a0 x0 I
<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>
2 r; m) B0 H5 \. l7 P7 d<P >原因:循环条件和对变量<FONT face="Times New Roman">left</FONT>值的修改有错误。<o:p></o:p></P>
, f# m, H2 Q7 h {<P >(<FONT face="Times New Roman">5</FONT>)此算法正确。<o:p></o:p></P>
- G: r4 [2 V9 r T/ b. w( Z1 C<P >证明:当<FONT face="Times New Roman">n=0</FONT>或<FONT face="Times New Roman">n=1</FONT>时,算法显然正确。<o:p></o:p></P>
: w( M; x& I+ }8 Y<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>
; Z5 q" X2 V) F0 E4 }: T- i<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>
1 `9 g5 b/ F) }
<P >即:middle > left成立。<o:p></o:p></P>
) d! r h. w/ d5 m1 V7 P( 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>
+ w7 Y0 e* n/ M: f, s' t
<P >∴left < middle ≤ right恒成立。<o:p></o:p></P>
# \6 c {. I; A
<P >因此,每次循环之后,right与left之差必然减小,在有限次循环后,必有left = right条件成立,从而循环结束。<o:p></o:p></P>
: ?. J# {/ V2 m$ x4 r<P >如果x值与数组a的某个元素值相等,则在循环结束时显然有x = a
且x = a
成立,否则x ≠a
,即未找到x,<o:p></o:p></P>
B9 j/ H4 i& o8 b2 a<P >∴返回结果正确。<o:p></o:p></P>
' u6 R6 G. Y, E. s4 s2 u<P >(6)算法6是错误的。<o:p></o:p></P>
6 [5 F! T# a8 c- Y
<P >当执行到某次循环x = a[middle]成立时,再执行if 语句中的<o:p></o:p></P>
8 z$ R# E$ V9 u
<P >left = middle + 1;<o:p></o:p></P>
% C; Z- y8 q/ N/ ?% e6 ~! R( E8 g
<P >就把结果丢失了,导致错误。而且还可能会导致下标越界错误。例如:<o:p></o:p></P>
4 u' E: W1 C0 N+ X$ N<P >当n = 2且x = a[1]时即会出现这些情况。<o:p></o:p></P>
5 k8 q: U) m# s
<P >原因:if 语句中的left = middle + 1;应改为left = middle;<o:p></o:p></P>
% a s2 q3 [% h9 A( A<P >(7)算法7是错误的。<o:p></o:p></P>
& O9 D# n* l6 g1 i
<P >在循环过程中,一旦出现<o:p></o:p></P>
$ p8 z) v) z( g
<P >a
≤ x < a[left + 1],则必进入死循环。<o:p></o:p></P>
/ i+ o. F8 |4 e/ ?' [<P >原因:right值的修改不正确。<o:p></o:p></P>