<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>
3 A! A/ z: s/ i
<P ><FONT face="Times New Roman">}//if</FONT></P>
8 D' q1 E7 W* b! e6 o# h; F
<P ><FONT face="Times New Roman">return –1;</FONT></P>
5 F( c& o) M6 y) B9 [0 v8 k5 Y<P ><FONT face="Times New Roman">}</FONT></P>
, F# y0 b5 t) j: g9 P- [
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
: M; Y, l+ Y% c0 j% y<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
2 s9 u' `- ^' h" s2 H9 B n2 S6 l<P ><FONT face="Times New Roman">public static int binarySearch5(int[] a, int x, int n)</FONT></P>
" ]- x4 t; s8 p* M% b) Y<P ><FONT face="Times New Roman">{</FONT></P>
% n$ V) {8 q2 l2 r) u; R' D
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
( |3 Q) b9 |7 b<P ><FONT face="Times New Roman"> {</FONT></P>
+ s: v! [% |* w: X; k
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
5 d F* Q" B5 `" h% s; u<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
# S5 Z" \) L8 n# U! `<P ><FONT face="Times New Roman"> {</FONT></P>
* W" n- f0 I/ G/ I( P X0 [ Q
<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
$ r1 A8 P' ]- g6 a<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
& {0 l; X0 P$ `- n( f% Y7 m D
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
& ? O ]& m# x+ ^<P ><FONT face="Times New Roman">}//while</FONT></P>
4 H5 `( K+ M- a3 O& u9 m+ U* N, G
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
* c8 Y4 W. P% _% G
<P ><FONT face="Times New Roman">}//if</FONT></P>
1 [3 x7 v' @7 Q5 e
<P ><FONT face="Times New Roman">return –1;</FONT></P>
$ q( y1 \/ }3 z W6 g3 D7 W" p<P ><FONT face="Times New Roman">}</FONT></P>
; B6 q1 H4 I& d2 u' }4 m
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
) r" O! J0 [1 E2 V' u) ^/ Y+ I
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
; r* e! l. p4 m- k
<P ><FONT face="Times New Roman">public static int binarySearch6(int[] a, int x, int n)</FONT></P>
3 t" Z8 ~2 H' R1 E; K' U<P ><FONT face="Times New Roman">{</FONT></P>
1 z& y& Q5 X4 `- J$ f+ t! V
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
; V1 o% Y5 }2 F, T8 C# L<P ><FONT face="Times New Roman"> {</FONT></P>
: U! P" B& S) ~7 V, r
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
3 _1 k9 ~/ D0 b0 X- j: G: F<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
" L! G1 N5 z- s' \1 m4 @/ c+ n+ l5 |
<P ><FONT face="Times New Roman"> {</FONT></P>
7 X! v* l2 H1 j, K3 n1 |<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
7 ?% I( s! E& |' u" a
<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
2 I" v$ M* \8 Q5 X<P ><FONT face="Times New Roman">else left = middle + 1;</FONT></P>
7 W9 `% w2 F2 S<P ><FONT face="Times New Roman">}//while</FONT></P>
$ e) m$ ] ?% E- w" O' c# A% `6 N
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
+ I8 B( @( m) ?5 s+ U4 ^! u$ k w8 ]<P ><FONT face="Times New Roman">}//if</FONT></P>
% l( m' B* Y3 p. f5 V( B
<P ><FONT face="Times New Roman">return –1;</FONT></P>
- y9 l' V& u5 y) ?2 \0 a<P ><FONT face="Times New Roman">}</FONT></P>
* [8 Z K. d& T/ i" n# O<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
3 p5 f9 N0 }7 U- O7 u& J' P: A: T
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
# ?# |( b8 p% x, m<P ><FONT face="Times New Roman">public static int binarySearch7(int[] a, int x, int n)</FONT></P>
( }) N6 J9 Z! s3 ]0 Z" u/ n6 E+ |
<P ><FONT face="Times New Roman">{</FONT></P>
- y5 H0 q4 H- c<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
* O5 c0 Q! r( s& p, `& L( b0 {
<P ><FONT face="Times New Roman"> {</FONT></P>
; i# Z/ n- p4 X! A<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
4 H6 ^/ U0 G: }2 X4 v9 i0 r<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
d& j7 F# R4 K% Z- l<P ><FONT face="Times New Roman"> {</FONT></P>
3 _, x! w1 i1 \! ~! T
<P ><FONT face="Times New Roman"> int middle = (left + right +1) / 2;</FONT></P>
- p+ k0 F" j& e d' x$ {<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle;</FONT></P>
; f& a, B5 T: q<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
! u" E8 B- S) W/ [4 d5 g- ^' G<P ><FONT face="Times New Roman">}//while</FONT></P>
- c, p/ s& v. D
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
: n3 k) V8 h( s5 W<P ><FONT face="Times New Roman">}//if</FONT></P>
1 k4 \$ M' a: Y) q: b" O& d* a
<P ><FONT face="Times New Roman">return –1;</FONT></P>
: d. ^. r& X3 m5 O
<P ><FONT face="Times New Roman">}</FONT></P>
* ? a7 _/ h/ y6 Q! ^: ?) R<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
) s) v0 V- z& {. t, N<P >解:(<FONT face="Times New Roman">1</FONT>)算法<FONT face="Times New Roman">1</FONT>不正确。<o:p></o:p></P>
6 K% D2 S7 ~- R; U
<P >当在数组<FONT face="Times New Roman">a</FONT>中找不到与<FONT face="Times New Roman">x</FONT>相等的元素时,算法将进入死循环状态。<o:p></o:p></P>
( B! v, s1 I6 @$ |+ [: T1 y" h9 R/ d<P >原因:每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改不正确。应修改如下:<o:p></o:p></P>
/ w0 L% T2 u: I$ u1 P$ f5 I
<P ><FONT face="Times New Roman">if(x > a[middle]) left = middle + 1;<o:p></o:p></FONT></P>
# D- C; d) v, e+ ~ e<P ><FONT face="Times New Roman"> else right = middle - 1;<o:p></o:p></FONT></P>
" U/ N6 C! k" u3 ^6 ?% m0 P
<P >(<FONT face="Times New Roman">2</FONT>)算法<FONT face="Times New Roman">2</FONT>不正确。<o:p></o:p></P>
1 b( E# p1 B1 u8 g- N6 ]0 a- B8 W# 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>
3 m! _/ I7 Z; Y" E<P >另外,当<FONT face="Times New Roman">n=0</FONT>时执行<FONT face="Times New Roman">if(x == a
)...</FONT>时将出现下标越界错误。<o:p></o:p></P>
7 l' O/ Y0 Q) F' B- C. ]<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>
, y2 J. ~! }/ N. F. R<P >(<FONT face="Times New Roman">3</FONT>)算法<FONT face="Times New Roman">3</FONT>不正确。<o:p></o:p></P>
( T% b! P# X6 m" n& _
<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 ?% t( Y u# k! r# `) T% J: n<P >原因:与算法<FONT face="Times New Roman">2</FONT>相同。<o:p></o:p></P>
' ]2 Z! b6 Y. R
<P >(<FONT face="Times New Roman">4</FONT>)算法<FONT face="Times New Roman">4</FONT>不正确。<o:p></o:p></P>
3 Y& w& \* I9 \$ D9 L; u
<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>
3 k/ Q' P* N) c$ l& j% E& y<P >原因:循环条件和对变量<FONT face="Times New Roman">left</FONT>值的修改有错误。<o:p></o:p></P>
2 L i% s3 u# D. G% H$ d B2 |# z+ }2 Q
<P >(<FONT face="Times New Roman">5</FONT>)此算法正确。<o:p></o:p></P>
; ^; q K( ~# {
<P >证明:当<FONT face="Times New Roman">n=0</FONT>或<FONT face="Times New Roman">n=1</FONT>时,算法显然正确。<o:p></o:p></P>
+ Z! W. I2 d: t! M' q3 V8 M! o4 ]
<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 o% h' e- b7 ~4 C% `<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>
l: e; O( H: I5 e<P >即:middle > left成立。<o:p></o:p></P>
& H4 _0 p) V6 I8 x6 G9 I5 M
<P >且<FONT face="Times New Roman">middle = (left + right + 1) / 2 = [(left + 1) + right] / 2 </FONT>≤ 2right / 2 = right,<o:p></o:p></P>
6 N/ V: o7 X9 Y1 M
<P >∴left < middle ≤ right恒成立。<o:p></o:p></P>
1 Q I/ L: E% I
<P >因此,每次循环之后,right与left之差必然减小,在有限次循环后,必有left = right条件成立,从而循环结束。<o:p></o:p></P>
5 [0 a9 i0 f9 \1 c0 n& I( [" @<P >如果x值与数组a的某个元素值相等,则在循环结束时显然有x = a
且x = a
成立,否则x ≠a
,即未找到x,<o:p></o:p></P>
# t6 H* l& s3 [; C6 C9 d2 s<P >∴返回结果正确。<o:p></o:p></P>
; P0 ?9 {2 M, M0 a% H<P >(6)算法6是错误的。<o:p></o:p></P>
" E+ W, [* v* s5 T<P >当执行到某次循环x = a[middle]成立时,再执行if 语句中的<o:p></o:p></P>
8 g( t- v" Q4 x3 g
<P >left = middle + 1;<o:p></o:p></P>
) S: a# l# _7 s6 a6 m' z<P >就把结果丢失了,导致错误。而且还可能会导致下标越界错误。例如:<o:p></o:p></P>
& }5 ]+ y7 `+ Y9 s3 a/ Z
<P >当n = 2且x = a[1]时即会出现这些情况。<o:p></o:p></P>
7 l& E/ `( U3 p, l5 M8 s" Y( d) b- {
<P >原因:if 语句中的left = middle + 1;应改为left = middle;<o:p></o:p></P>
5 M0 G7 ^! {/ K4 M" a* b9 k
<P >(7)算法7是错误的。<o:p></o:p></P>
% R8 m9 Z. z% P" K, b/ x* N3 t" s
<P >在循环过程中,一旦出现<o:p></o:p></P>
% Z8 s# i9 g/ G1 I<P >a
≤ x < a[left + 1],则必进入死循环。<o:p></o:p></P>
& W& R+ w+ Y2 S9 ?. D* ~<P >原因:right值的修改不正确。<o:p></o:p></P>