<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>
9 t q3 C( D6 C, Y' X
<P ><FONT face="Times New Roman">}//if</FONT></P>
4 z5 p. T0 ]" A
<P ><FONT face="Times New Roman">return –1;</FONT></P>
& E5 z4 Z$ H% @* s3 n5 a# S<P ><FONT face="Times New Roman">}</FONT></P>
" q* o0 z' b% }5 | A<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
1 s' y6 e c" \) b9 o: P0 i
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
+ F: q5 H7 T9 V/ h+ a
<P ><FONT face="Times New Roman">public static int binarySearch5(int[] a, int x, int n)</FONT></P>
# F* x# i0 E2 i# r5 y: t
<P ><FONT face="Times New Roman">{</FONT></P>
( {" t, x- |7 i, B5 Z6 @
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
: s0 m; J. f2 X6 Z ]
<P ><FONT face="Times New Roman"> {</FONT></P>
8 Z! f( f: `, @0 l6 h<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
6 p' B- d$ H `) r$ ^) y<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
. Y3 [1 B8 L3 y: z7 ^4 X4 X
<P ><FONT face="Times New Roman"> {</FONT></P>
/ L2 Y: T0 \+ j; p- [
<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
: V6 k4 [- l7 Q, {<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
/ Q! T, B- I: A$ }4 }
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
1 z) a* P- z7 `<P ><FONT face="Times New Roman">}//while</FONT></P>
& L' b* p( z! q2 x9 R) p<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
, I4 _8 v, e0 T/ r e: j9 N<P ><FONT face="Times New Roman">}//if</FONT></P>
- h- o( e7 M2 W/ g) Y! i
<P ><FONT face="Times New Roman">return –1;</FONT></P>
! E3 Y& g: w# E( i+ n& W8 w
<P ><FONT face="Times New Roman">}</FONT></P>
1 H$ t1 i9 K0 @9 p0 R
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
6 B% w4 o' J$ I0 D
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
7 i$ T$ e# w4 {: T& _" A7 Y6 `
<P ><FONT face="Times New Roman">public static int binarySearch6(int[] a, int x, int n)</FONT></P>
5 h s, N- ^3 j# R) A& w' S<P ><FONT face="Times New Roman">{</FONT></P>
1 ^& l) m% m! g g<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
! w7 q, p3 Y( |% ?5 |<P ><FONT face="Times New Roman"> {</FONT></P>
+ U! B. ~- m' n$ [! h<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
( R# c0 F. ]# n) p8 h<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
# @9 l0 Q9 T6 |! i& i+ L
<P ><FONT face="Times New Roman"> {</FONT></P>
3 N( t3 t4 G8 g( i+ O<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
" _* D2 z* U% R8 B<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
( U/ R3 _" \. X3 K1 i6 Y- k0 {<P ><FONT face="Times New Roman">else left = middle + 1;</FONT></P>
0 L! J" R8 c4 V% o3 [1 _<P ><FONT face="Times New Roman">}//while</FONT></P>
6 w4 i9 ]2 |) E0 q* {<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
8 X" g& c& d( i; _" ]<P ><FONT face="Times New Roman">}//if</FONT></P>
9 x# ^2 u( d- P7 f4 |<P ><FONT face="Times New Roman">return –1;</FONT></P>
/ }+ Z1 s. a0 ]3 X<P ><FONT face="Times New Roman">}</FONT></P>
. n; p! s4 d) }0 Q' q* }<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
$ M: Y8 P% u! z; G+ S# e3 }6 N<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
1 n4 T" \* |6 W5 q. S0 [
<P ><FONT face="Times New Roman">public static int binarySearch7(int[] a, int x, int n)</FONT></P>
0 ^7 v9 C* F* r/ m. J9 R) D) F<P ><FONT face="Times New Roman">{</FONT></P>
" ?/ ~% R, w, D( }9 z<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
' i' @/ C% w/ E7 H, p e
<P ><FONT face="Times New Roman"> {</FONT></P>
- d3 I' T9 d) _' s; C
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
! K- K6 _8 |/ Z( S<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
( \! D+ |+ x( g( t3 ?
<P ><FONT face="Times New Roman"> {</FONT></P>
1 O* f5 X! e. m* [- T<P ><FONT face="Times New Roman"> int middle = (left + right +1) / 2;</FONT></P>
) v' K5 x, \8 ~( D. n$ T- l" J
<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle;</FONT></P>
. Q$ O1 _& F2 \7 b0 h8 P
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
" _1 m( @3 m1 A0 w' L. u
<P ><FONT face="Times New Roman">}//while</FONT></P>
6 k' d( z2 |! u' r, v0 E<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
4 A, n( _. p4 |8 g9 s0 @
<P ><FONT face="Times New Roman">}//if</FONT></P>
2 m; J/ X/ w4 y: p
<P ><FONT face="Times New Roman">return –1;</FONT></P>
3 x3 a/ F3 T+ k<P ><FONT face="Times New Roman">}</FONT></P>
# l5 q8 a" k& U, N. ^
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
9 U' h. x( X- ~* p<P >解:(<FONT face="Times New Roman">1</FONT>)算法<FONT face="Times New Roman">1</FONT>不正确。<o:p></o:p></P>
6 p8 _' Q0 S) V" X2 P
<P >当在数组<FONT face="Times New Roman">a</FONT>中找不到与<FONT face="Times New Roman">x</FONT>相等的元素时,算法将进入死循环状态。<o:p></o:p></P>
- N& e, E' a9 Z" X0 T( I: R
<P >原因:每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改不正确。应修改如下:<o:p></o:p></P>
7 z. x a6 m, U, h0 h; A
<P ><FONT face="Times New Roman">if(x > a[middle]) left = middle + 1;<o:p></o:p></FONT></P>
: T, e D; q! W1 [5 c
<P ><FONT face="Times New Roman"> else right = middle - 1;<o:p></o:p></FONT></P>
4 X d+ w1 g# s! a$ C q+ T<P >(<FONT face="Times New Roman">2</FONT>)算法<FONT face="Times New Roman">2</FONT>不正确。<o:p></o:p></P>
3 n5 c+ o+ ^0 s5 g2 w
<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>
, a. D2 c, ?( Q8 N<P >另外,当<FONT face="Times New Roman">n=0</FONT>时执行<FONT face="Times New Roman">if(x == a
)...</FONT>时将出现下标越界错误。<o:p></o:p></P>
8 K! [5 M; h( \' i4 x) Y1 o<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>
( H' Q7 j2 ?" E$ A1 s- a) p
<P >(<FONT face="Times New Roman">3</FONT>)算法<FONT face="Times New Roman">3</FONT>不正确。<o:p></o:p></P>
- G! V$ C+ W- R
<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>
# v' `8 E! ?9 T1 W9 I5 g, ?<P >原因:与算法<FONT face="Times New Roman">2</FONT>相同。<o:p></o:p></P>
" |" j; D1 F7 x) q& i5 f<P >(<FONT face="Times New Roman">4</FONT>)算法<FONT face="Times New Roman">4</FONT>不正确。<o:p></o:p></P>
: Z* E6 V6 p: I0 |! S5 E/ ]
<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>
7 N! C0 d& {- U4 t0 v9 e
<P >原因:循环条件和对变量<FONT face="Times New Roman">left</FONT>值的修改有错误。<o:p></o:p></P>
" K9 d' O, b( D- A' S. K) c5 X* }<P >(<FONT face="Times New Roman">5</FONT>)此算法正确。<o:p></o:p></P>
0 y" ?: Y( g+ H9 f
<P >证明:当<FONT face="Times New Roman">n=0</FONT>或<FONT face="Times New Roman">n=1</FONT>时,算法显然正确。<o:p></o:p></P>
8 W8 m; \, z. |9 q5 Z
<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 j3 c6 d4 V2 n/ S! m; r3 \- D
<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>
/ p4 S, r" p4 y" e<P >即:middle > left成立。<o:p></o:p></P>
+ w9 v& U% P: H) @8 k8 C<P >且<FONT face="Times New Roman">middle = (left + right + 1) / 2 = [(left + 1) + right] / 2 </FONT>≤ 2right / 2 = right,<o:p></o:p></P>
( A! |3 I" ^4 G) d. x; F% e: u. h9 u" }
<P >∴left < middle ≤ right恒成立。<o:p></o:p></P>
; m# V% @1 u$ K3 l
<P >因此,每次循环之后,right与left之差必然减小,在有限次循环后,必有left = right条件成立,从而循环结束。<o:p></o:p></P>
0 ?/ O3 J2 y5 i' z. U
<P >如果x值与数组a的某个元素值相等,则在循环结束时显然有x = a
且x = a
成立,否则x ≠a
,即未找到x,<o:p></o:p></P>
3 ~2 v2 c8 R" Q' X4 j# Q4 q
<P >∴返回结果正确。<o:p></o:p></P>
. e7 o2 I/ l7 \5 r
<P >(6)算法6是错误的。<o:p></o:p></P>
9 r9 n' M# G( A6 s1 i4 o<P >当执行到某次循环x = a[middle]成立时,再执行if 语句中的<o:p></o:p></P>
+ H: j6 F+ W* j3 Q2 O6 b<P >left = middle + 1;<o:p></o:p></P>
/ k/ v8 Z* s3 E: R; m5 x: D
<P >就把结果丢失了,导致错误。而且还可能会导致下标越界错误。例如:<o:p></o:p></P>
1 g7 \% N% V! i1 p: [; ~
<P >当n = 2且x = a[1]时即会出现这些情况。<o:p></o:p></P>
4 f! s4 T% X' q2 r# X" D( E3 n<P >原因:if 语句中的left = middle + 1;应改为left = middle;<o:p></o:p></P>
1 L: U3 A9 ?; x. ^, G( V) H<P >(7)算法7是错误的。<o:p></o:p></P>
: e. B0 `9 s* P4 ^' s, o' r- }
<P >在循环过程中,一旦出现<o:p></o:p></P>
! _! h l/ g J7 g7 m: _* x<P >a
≤ x < a[left + 1],则必进入死循环。<o:p></o:p></P>) S4 j" [1 e$ U! e& p( p$ I
<P >原因:right值的修改不正确。<o:p></o:p></P>