<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>
, X4 c2 {/ g6 H/ S9 k& c
<P ><FONT face="Times New Roman">}//if</FONT></P>
% [7 N* t2 P! B, G9 ~, {; C# ~
<P ><FONT face="Times New Roman">return –1;</FONT></P>
; ~4 A+ x! q: @! t; g% W
<P ><FONT face="Times New Roman">}</FONT></P>
T: l- N5 r7 F# {0 M: U/ K/ _* c<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
0 G0 r0 p9 T6 o. \7 @, _<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
) m1 _9 Q: y8 N( f<P ><FONT face="Times New Roman">public static int binarySearch5(int[] a, int x, int n)</FONT></P>
! P- ?! T$ {% g8 e6 n
<P ><FONT face="Times New Roman">{</FONT></P>
9 ~7 H$ x) H* t<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
2 y8 e; H; E& y. h
<P ><FONT face="Times New Roman"> {</FONT></P>
5 }! u, k9 L' A) @<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
: T/ o8 {/ h% D; {& }
<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
; w; _, S; l7 X
<P ><FONT face="Times New Roman"> {</FONT></P>
& @4 t8 u/ d4 K* M$ p' @3 e5 T# C" w
<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
1 D& E' h6 G) C8 Y. z7 R! e1 T7 U
<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
$ V+ q. W: {) X<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
+ W8 E i Q: H2 f<P ><FONT face="Times New Roman">}//while</FONT></P>
4 `( ?; [* v5 ^<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
3 U+ f6 u) y% u7 F, K1 c" i& I, D
<P ><FONT face="Times New Roman">}//if</FONT></P>
5 [7 F0 S# Y3 [7 ?- \5 T
<P ><FONT face="Times New Roman">return –1;</FONT></P>
9 {- b* J' t) F6 }- k- P
<P ><FONT face="Times New Roman">}</FONT></P>
% U# q1 z$ k, Q- f* C$ ^" r/ x
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
! v% P' ^- z6 w5 X6 O* l" m& F<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
% J2 ?* d7 T# J6 a9 ?0 Z<P ><FONT face="Times New Roman">public static int binarySearch6(int[] a, int x, int n)</FONT></P>
E) i5 W, V3 `0 R M
<P ><FONT face="Times New Roman">{</FONT></P>
( T' E3 R5 w$ M6 {, T# x<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
4 k: I9 m+ A! v/ F; M
<P ><FONT face="Times New Roman"> {</FONT></P>
5 Z4 v6 Y: b+ s3 M% x. V+ {8 y$ j; t<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
# b; x1 Q+ X- |
<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
. I6 y+ ]1 h7 u- J8 f) u# _$ H' t
<P ><FONT face="Times New Roman"> {</FONT></P>
8 y) n+ U; c. ]' C% z
<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
: k" v. {# h& ~! F/ `8 D
<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
- o* o5 N9 F. U- D4 ]$ H<P ><FONT face="Times New Roman">else left = middle + 1;</FONT></P>
9 {8 S% `+ [+ m<P ><FONT face="Times New Roman">}//while</FONT></P>
3 i1 ?- m/ G8 ^/ s2 Z<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
6 |) g4 S |7 [) o2 P* K4 W2 s
<P ><FONT face="Times New Roman">}//if</FONT></P>
; @4 V$ s0 ^! ^3 _<P ><FONT face="Times New Roman">return –1;</FONT></P>
* Q3 n! G8 i3 d& f9 _. ]- ~
<P ><FONT face="Times New Roman">}</FONT></P>
! E% ?1 E( t3 F<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
6 m0 I: \- _: b7 m& d<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
* M& W4 d$ Y+ S! c) y<P ><FONT face="Times New Roman">public static int binarySearch7(int[] a, int x, int n)</FONT></P>
; ~- P/ u# Z4 ] H, x
<P ><FONT face="Times New Roman">{</FONT></P>
4 M( {" y' f/ j
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
~& A) p0 C& y4 ]& s<P ><FONT face="Times New Roman"> {</FONT></P>
3 |# K( P- V9 n( ?
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
7 ~9 m2 t+ ]) A! \( B
<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
% h- ~. C) ~" a
<P ><FONT face="Times New Roman"> {</FONT></P>
% Z4 `' N' U7 }8 H+ H0 |0 h1 o<P ><FONT face="Times New Roman"> int middle = (left + right +1) / 2;</FONT></P>
! z0 N2 i9 x0 d/ k$ E3 e<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle;</FONT></P>
) k/ ^+ k: w2 e1 W
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
, w. U9 R8 T# A3 Y! k5 K<P ><FONT face="Times New Roman">}//while</FONT></P>
! k0 A5 p: m* ~) c<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
. t0 q2 C5 n) t3 ^4 {# O
<P ><FONT face="Times New Roman">}//if</FONT></P>
C' w: B7 Z% G<P ><FONT face="Times New Roman">return –1;</FONT></P>
8 h& i# U; T. A; f" @% O% Y- z9 Z
<P ><FONT face="Times New Roman">}</FONT></P>
0 p9 g" f7 {3 F7 C. T4 l<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
s# M" ?2 [$ R3 T" b& d: ^% E% O
<P >解:(<FONT face="Times New Roman">1</FONT>)算法<FONT face="Times New Roman">1</FONT>不正确。<o:p></o:p></P>
* K) S. Y: E- a) I) Z: z6 x
<P >当在数组<FONT face="Times New Roman">a</FONT>中找不到与<FONT face="Times New Roman">x</FONT>相等的元素时,算法将进入死循环状态。<o:p></o:p></P>
8 Z. V2 E8 b$ \7 e5 L" e6 { K$ y
<P >原因:每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改不正确。应修改如下:<o:p></o:p></P>
; _# J% w- d8 b) E* q0 f<P ><FONT face="Times New Roman">if(x > a[middle]) left = middle + 1;<o:p></o:p></FONT></P>
8 A: H8 Z( r0 z<P ><FONT face="Times New Roman"> else right = middle - 1;<o:p></o:p></FONT></P>
: Z; R$ ?+ a4 w0 Y( [
<P >(<FONT face="Times New Roman">2</FONT>)算法<FONT face="Times New Roman">2</FONT>不正确。<o:p></o:p></P>
* \3 k. A" ^6 n5 A/ a- P1 ]! l1 [' C<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>
) b4 n- u7 Y7 I% E<P >另外,当<FONT face="Times New Roman">n=0</FONT>时执行<FONT face="Times New Roman">if(x == a
)...</FONT>时将出现下标越界错误。<o:p></o:p></P>
2 W' n* H8 b5 \4 u$ g<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>
+ C( d+ N% A$ o/ O) m% g6 @5 E
<P >(<FONT face="Times New Roman">3</FONT>)算法<FONT face="Times New Roman">3</FONT>不正确。<o:p></o:p></P>
& I$ ~% k! M; j3 y I" [<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>
' C1 `$ m; t% B! h6 O
<P >原因:与算法<FONT face="Times New Roman">2</FONT>相同。<o:p></o:p></P>
- M+ l) z! ~2 M5 v) ~<P >(<FONT face="Times New Roman">4</FONT>)算法<FONT face="Times New Roman">4</FONT>不正确。<o:p></o:p></P>
/ |4 W0 ]" K! Q
<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>
! e& a$ ~+ x# O( D<P >原因:循环条件和对变量<FONT face="Times New Roman">left</FONT>值的修改有错误。<o:p></o:p></P>
N- E" c: H3 G, M/ k7 t- ?
<P >(<FONT face="Times New Roman">5</FONT>)此算法正确。<o:p></o:p></P>
5 ~$ e$ Y. F: P* A
<P >证明:当<FONT face="Times New Roman">n=0</FONT>或<FONT face="Times New Roman">n=1</FONT>时,算法显然正确。<o:p></o:p></P>
8 {: O+ G; l' m- ~; s( L<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>
! ?3 O6 s6 i1 r$ W4 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>
# y2 [) K, _6 v% E! B
<P >即:middle > left成立。<o:p></o:p></P>
! ^. G, ^- N6 B* R<P >且<FONT face="Times New Roman">middle = (left + right + 1) / 2 = [(left + 1) + right] / 2 </FONT>≤ 2right / 2 = right,<o:p></o:p></P>
* O" L7 H6 a5 Q6 \- B
<P >∴left < middle ≤ right恒成立。<o:p></o:p></P>
) ^! e% f# m& K+ C W" W- t" c
<P >因此,每次循环之后,right与left之差必然减小,在有限次循环后,必有left = right条件成立,从而循环结束。<o:p></o:p></P>
8 s. U% p3 ^; M6 y, [" N8 D
<P >如果x值与数组a的某个元素值相等,则在循环结束时显然有x = a
且x = a
成立,否则x ≠a
,即未找到x,<o:p></o:p></P>
0 I* i9 _. ~" i& D6 Z+ A- f<P >∴返回结果正确。<o:p></o:p></P>
+ f$ J* n* H, l0 H( W0 T<P >(6)算法6是错误的。<o:p></o:p></P>
9 B) E( ^" `, [; X- l2 I# O2 \<P >当执行到某次循环x = a[middle]成立时,再执行if 语句中的<o:p></o:p></P>
7 q3 R2 p! C4 `, h3 z5 E' u/ v<P >left = middle + 1;<o:p></o:p></P>
& t! G7 J6 d4 Q' C& F) h
<P >就把结果丢失了,导致错误。而且还可能会导致下标越界错误。例如:<o:p></o:p></P>
K5 W- g" l' u+ S. x( b5 s, K! U
<P >当n = 2且x = a[1]时即会出现这些情况。<o:p></o:p></P>
f! s) l3 \: v3 Y9 g; e<P >原因:if 语句中的left = middle + 1;应改为left = middle;<o:p></o:p></P>
! m$ I! V7 W% o! t6 o
<P >(7)算法7是错误的。<o:p></o:p></P>
: S0 S' D9 W* y+ H<P >在循环过程中,一旦出现<o:p></o:p></P>
, R8 c, J2 C1 S; W- H
<P >a
≤ x < a[left + 1],则必进入死循环。<o:p></o:p></P>
8 {1 k2 F# s: \- q) V<P >原因:right值的修改不正确。<o:p></o:p></P>