<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>
/ n& s. V4 Z% p% C0 o& n
<P ><FONT face="Times New Roman">}//if</FONT></P>
3 v- L r0 q) |* r1 r! q
<P ><FONT face="Times New Roman">return –1;</FONT></P>
3 L4 m: ~% o: u$ G2 ^3 f6 G7 B4 y
<P ><FONT face="Times New Roman">}</FONT></P>
2 P8 ` ? U' s
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
- T8 Y) c8 M3 G+ o' I<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
3 i/ ^1 ~7 `5 K- t3 a
<P ><FONT face="Times New Roman">public static int binarySearch5(int[] a, int x, int n)</FONT></P>
6 `" _$ a `1 G3 |, p3 \
<P ><FONT face="Times New Roman">{</FONT></P>
9 _, i; [/ e( m6 |% |' y
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
6 G% C6 J! o8 f4 h<P ><FONT face="Times New Roman"> {</FONT></P>
2 o6 f* u; [- K, l; }; A0 X+ x
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
8 U1 o4 D, o* S8 G, D3 |<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
+ w) }, t& w# H: H1 y% [<P ><FONT face="Times New Roman"> {</FONT></P>
1 R* n+ B' V& R8 s! n! i; c+ C
<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
! V% }: j3 g9 a' U: |+ u+ T3 V
<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
& c! w3 k& U% c<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
9 P/ H5 ? i+ C3 u
<P ><FONT face="Times New Roman">}//while</FONT></P>
9 b0 j3 n5 c3 r1 a- e
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
; d, U! G+ Q; K+ V- b<P ><FONT face="Times New Roman">}//if</FONT></P>
4 x/ h! A4 x& F: k2 `
<P ><FONT face="Times New Roman">return –1;</FONT></P>
) t" Q+ P6 \- i; U9 Z" G
<P ><FONT face="Times New Roman">}</FONT></P>
4 n+ n/ h" H1 @4 k% M3 X<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
8 w( F1 j1 J$ c' ^" G6 f- L
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
- W( t% |" K1 N* l
<P ><FONT face="Times New Roman">public static int binarySearch6(int[] a, int x, int n)</FONT></P>
+ _: V, S$ F/ |% H<P ><FONT face="Times New Roman">{</FONT></P>
+ h9 J+ e Y4 U9 O* J* `; ]' A- x<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
( y6 q; Z! _- u/ e) h1 a
<P ><FONT face="Times New Roman"> {</FONT></P>
) x. ~* L v8 I2 ? r. r<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
) V! Y; d3 K+ F, e# }
<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
b+ S" z0 a8 Z# E" ` J4 x<P ><FONT face="Times New Roman"> {</FONT></P>
" ?+ M" p2 O7 L. Z% Q<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
1 `4 q/ O/ \* h! |<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
, P r- g8 [5 t$ j* p
<P ><FONT face="Times New Roman">else left = middle + 1;</FONT></P>
- g) A0 Y) N3 `6 v<P ><FONT face="Times New Roman">}//while</FONT></P>
+ W% o( S2 \7 q% C<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
4 o" s5 ~8 F4 x8 ~9 S& g
<P ><FONT face="Times New Roman">}//if</FONT></P>
! P; K, Q7 H1 ~. f1 N, m S' o<P ><FONT face="Times New Roman">return –1;</FONT></P>
3 J, s- A8 Z) G* {1 ]/ t
<P ><FONT face="Times New Roman">}</FONT></P>
7 q y$ a) [- W8 K, S<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
7 ~- P X8 o! W* d' ^3 v8 B) i- b0 O F
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
5 e" m% l+ t7 S5 n: Y8 @<P ><FONT face="Times New Roman">public static int binarySearch7(int[] a, int x, int n)</FONT></P>
) r7 ?9 h4 D% V- K' N& S
<P ><FONT face="Times New Roman">{</FONT></P>
. s, B6 z) B6 B; v( D2 X. U
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
$ X9 ?/ w% R4 s9 I# D+ l* U0 W<P ><FONT face="Times New Roman"> {</FONT></P>
4 H' S2 a# A+ t2 X5 [: a) h<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
3 L" q: h! i+ C Y7 X<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
0 O8 j8 ~: h: j3 ]! ]" s
<P ><FONT face="Times New Roman"> {</FONT></P>
" o( g# [1 c, j+ \3 _2 c<P ><FONT face="Times New Roman"> int middle = (left + right +1) / 2;</FONT></P>
5 y [; T" w* J: V, v; ?<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle;</FONT></P>
2 `( D6 P. {: ?) V2 S# p<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
, K o* D& Z! D$ h* g9 ^* `
<P ><FONT face="Times New Roman">}//while</FONT></P>
]( \6 x0 n7 H7 r/ D6 _<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
) }. u- N( V' v F7 b' H1 V N: A) a<P ><FONT face="Times New Roman">}//if</FONT></P>
' T, ~/ }7 c+ u. c<P ><FONT face="Times New Roman">return –1;</FONT></P>
0 g/ I9 P7 B% m2 b3 u8 v, ^3 f<P ><FONT face="Times New Roman">}</FONT></P>
1 i% N7 S7 z$ D
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
$ \7 S3 X9 F* J+ K- p+ a* p7 O<P >解:(<FONT face="Times New Roman">1</FONT>)算法<FONT face="Times New Roman">1</FONT>不正确。<o:p></o:p></P>
: g0 P# D$ i# M
<P >当在数组<FONT face="Times New Roman">a</FONT>中找不到与<FONT face="Times New Roman">x</FONT>相等的元素时,算法将进入死循环状态。<o:p></o:p></P>
7 b: G O. Z8 v z) j<P >原因:每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改不正确。应修改如下:<o:p></o:p></P>
3 A4 D7 ?2 p3 U
<P ><FONT face="Times New Roman">if(x > a[middle]) left = middle + 1;<o:p></o:p></FONT></P>
; Q! ^5 Q7 f) }" ?1 {: G<P ><FONT face="Times New Roman"> else right = middle - 1;<o:p></o:p></FONT></P>
1 A6 O/ T& z& }2 ?6 s2 y
<P >(<FONT face="Times New Roman">2</FONT>)算法<FONT face="Times New Roman">2</FONT>不正确。<o:p></o:p></P>
% n2 o/ f" u5 ^* F& A, b9 o9 u<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>
7 V u% Z$ G* B+ o1 k |4 ~
<P >另外,当<FONT face="Times New Roman">n=0</FONT>时执行<FONT face="Times New Roman">if(x == a
)...</FONT>时将出现下标越界错误。<o:p></o:p></P>
+ |4 U# K5 a; s! n/ U( j4 X
<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>
$ _' Y$ [" \- a# C9 n$ Z
<P >(<FONT face="Times New Roman">3</FONT>)算法<FONT face="Times New Roman">3</FONT>不正确。<o:p></o:p></P>
( C5 @4 Q5 x) v5 L<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>
3 z0 D* t0 c. B
<P >原因:与算法<FONT face="Times New Roman">2</FONT>相同。<o:p></o:p></P>
& S% f$ r) r' P7 r( J8 w<P >(<FONT face="Times New Roman">4</FONT>)算法<FONT face="Times New Roman">4</FONT>不正确。<o:p></o:p></P>
. o3 ? f9 B: Q, v/ ^* o7 G
<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>
' m* b/ F! I4 g8 o
<P >原因:循环条件和对变量<FONT face="Times New Roman">left</FONT>值的修改有错误。<o:p></o:p></P>
: f" E' M% l8 o' X9 V<P >(<FONT face="Times New Roman">5</FONT>)此算法正确。<o:p></o:p></P>
( B9 E! L6 h4 a& u
<P >证明:当<FONT face="Times New Roman">n=0</FONT>或<FONT face="Times New Roman">n=1</FONT>时,算法显然正确。<o:p></o:p></P>
' G% w( L3 T* h; T) f' s, b4 h
<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>
1 Q& L" c/ B: C) E/ Z2 x6 o
<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>
5 m/ C/ {, C; t, N( N1 D
<P >即:middle > left成立。<o:p></o:p></P>
7 I/ H; A. o2 h9 C. b) s
<P >且<FONT face="Times New Roman">middle = (left + right + 1) / 2 = [(left + 1) + right] / 2 </FONT>≤ 2right / 2 = right,<o:p></o:p></P>
; n! m: t: b6 g3 d<P >∴left < middle ≤ right恒成立。<o:p></o:p></P>
; P- W* F0 R0 T1 U( U. P
<P >因此,每次循环之后,right与left之差必然减小,在有限次循环后,必有left = right条件成立,从而循环结束。<o:p></o:p></P>
: p: |- S9 Y/ ]. M' ]
<P >如果x值与数组a的某个元素值相等,则在循环结束时显然有x = a
且x = a
成立,否则x ≠a
,即未找到x,<o:p></o:p></P>
a+ \1 A" u2 C( f0 E9 x<P >∴返回结果正确。<o:p></o:p></P>
$ B% u2 Z* Q) b1 E1 j
<P >(6)算法6是错误的。<o:p></o:p></P>
! l/ J9 g8 e0 M4 t0 I7 E. T0 n( U
<P >当执行到某次循环x = a[middle]成立时,再执行if 语句中的<o:p></o:p></P>
3 l O- A6 F( E2 V9 C3 ~
<P >left = middle + 1;<o:p></o:p></P>
8 A; o2 C! g: a* v1 R0 W9 c
<P >就把结果丢失了,导致错误。而且还可能会导致下标越界错误。例如:<o:p></o:p></P>
7 s, V/ N6 Y5 F
<P >当n = 2且x = a[1]时即会出现这些情况。<o:p></o:p></P>
7 ]8 |, [4 P5 R# H5 X/ L<P >原因:if 语句中的left = middle + 1;应改为left = middle;<o:p></o:p></P>
/ G4 P8 B; D+ E+ M<P >(7)算法7是错误的。<o:p></o:p></P>
% B. v' b( S; E" z<P >在循环过程中,一旦出现<o:p></o:p></P>
% ^4 W# r$ \/ N, L) }, x
<P >a
≤ x < a[left + 1],则必进入死循环。<o:p></o:p></P>
, S! B5 F; [% _1 j<P >原因:right值的修改不正确。<o:p></o:p></P>