<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>
$ y$ H( M2 l$ M# f8 e+ z5 l
<P ><FONT face="Times New Roman">}//if</FONT></P>
$ ^3 {1 G; D' ?( Q1 A. I7 f
<P ><FONT face="Times New Roman">return –1;</FONT></P>
; L9 N, m* {: r3 k<P ><FONT face="Times New Roman">}</FONT></P>
, d2 h, U$ m5 @<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
. O* j4 f" Y0 M& h/ ?$ S<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
. V6 b T9 q3 Q* F/ T
<P ><FONT face="Times New Roman">public static int binarySearch5(int[] a, int x, int n)</FONT></P>
) @8 \7 n2 `1 h2 g2 z5 u3 c* Y<P ><FONT face="Times New Roman">{</FONT></P>
+ A! g* v ?1 B
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
9 r* u3 q1 [1 b3 I$ W
<P ><FONT face="Times New Roman"> {</FONT></P>
3 {( N! o# |# D3 v<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
) N6 r/ _) K) J9 K<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
9 H4 \" R5 b2 n+ ]) b/ i
<P ><FONT face="Times New Roman"> {</FONT></P>
" K) Z! z @$ q' ^ C5 e3 B
<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
6 ]; ?9 _% D5 n6 W' d# l<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
& @1 V# U% F! Q
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
; ?+ `' a+ Q1 G<P ><FONT face="Times New Roman">}//while</FONT></P>
/ }1 F& f3 t3 w9 M/ A* C5 { k<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
2 a: ~" {/ ?5 z9 T, e
<P ><FONT face="Times New Roman">}//if</FONT></P>
/ y) y. H- i; X( M9 ]( ~<P ><FONT face="Times New Roman">return –1;</FONT></P>
( f. B5 u8 d, p& ]7 ~- f/ C
<P ><FONT face="Times New Roman">}</FONT></P>
+ E, b' V3 I5 f& L% }8 _0 Z% @<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
4 G0 E: [+ K8 J9 A( A4 e- n, H% z& |, v<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
, i% U4 n; J& |<P ><FONT face="Times New Roman">public static int binarySearch6(int[] a, int x, int n)</FONT></P>
! D: [( m. c' }0 d; D<P ><FONT face="Times New Roman">{</FONT></P>
9 Y! r, X/ u% K5 B8 ^/ @
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
; T& Z! j( f0 M. K$ u<P ><FONT face="Times New Roman"> {</FONT></P>
; Q' s3 v1 I: C8 R# Q
<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
' c) U( y0 ?) c: r6 d! I8 f0 x<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
/ w* r) k1 }! A; r6 v% e( H7 S
<P ><FONT face="Times New Roman"> {</FONT></P>
! d( w" i; C0 f$ w8 V
<P ><FONT face="Times New Roman"> int middle = (left + right + 1) / 2;</FONT></P>
( Q! i& }# n1 U$ _/ j" f<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle - 1;</FONT></P>
( |" [. V7 f4 j% _/ h( [ A
<P ><FONT face="Times New Roman">else left = middle + 1;</FONT></P>
, Z9 [; G U2 I! e0 s
<P ><FONT face="Times New Roman">}//while</FONT></P>
9 K4 P: ^ `& b; [<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
1 ^. B! @6 Z9 H# K6 d<P ><FONT face="Times New Roman">}//if</FONT></P>
5 J! \1 ]# `4 ~- v) P/ E' l<P ><FONT face="Times New Roman">return –1;</FONT></P>
! [& Y. e3 {$ ^9 {; _9 ?- J
<P ><FONT face="Times New Roman">}</FONT></P>
- o0 f" B. U f2 a/ M9 L8 K<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
2 m3 z1 I0 _" R2 Z( n<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
3 e- ~4 J( s, f% {1 w$ S
<P ><FONT face="Times New Roman">public static int binarySearch7(int[] a, int x, int n)</FONT></P>
8 X( s1 x. v( {) C
<P ><FONT face="Times New Roman">{</FONT></P>
0 E5 h! [% U2 `; z) e( v* v
<P ><FONT face="Times New Roman"> if(n > 0 && x >= a[0])</FONT></P>
9 c& M' _- J( e- P- J<P ><FONT face="Times New Roman"> {</FONT></P>
) R5 o* ]. T" l1 R<P ><FONT face="Times New Roman"> int left = 0, right = n-1;</FONT></P>
9 K3 t4 \+ x, A5 g- A" T<P ><FONT face="Times New Roman"> while(left < right)</FONT></P>
+ `4 u- b1 z& C, u3 m( @, P6 c% K
<P ><FONT face="Times New Roman"> {</FONT></P>
1 ?7 {# u7 q X6 `& G<P ><FONT face="Times New Roman"> int middle = (left + right +1) / 2;</FONT></P>
7 z1 o; E' r& y# P% x( s" r' ~5 f( l4 K6 Z<P ><FONT face="Times New Roman">if(x < a[middle]) right = middle;</FONT></P>
2 m+ B& z9 F# g* R8 P<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
4 c7 r5 l' A: u5 Q0 n
<P ><FONT face="Times New Roman">}//while</FONT></P>
8 J7 r) {' c9 v* A4 U- D: K( m
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
3 }# l+ Y3 X; a' J! j2 c
<P ><FONT face="Times New Roman">}//if</FONT></P>
, J% z5 ^6 P1 M; t" G9 M0 {<P ><FONT face="Times New Roman">return –1;</FONT></P>
: ^5 T* V4 R6 X4 x0 _
<P ><FONT face="Times New Roman">}</FONT></P>
8 h' { ^! L" t/ c. p* G$ W<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
! Q7 N* T& O5 U3 j g
<P >解:(<FONT face="Times New Roman">1</FONT>)算法<FONT face="Times New Roman">1</FONT>不正确。<o:p></o:p></P>
0 C* D4 e) ~/ c1 y<P >当在数组<FONT face="Times New Roman">a</FONT>中找不到与<FONT face="Times New Roman">x</FONT>相等的元素时,算法将进入死循环状态。<o:p></o:p></P>
" H- N. q$ J$ I; z
<P >原因:每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改不正确。应修改如下:<o:p></o:p></P>
* c* e. r" p# X" V' l6 e<P ><FONT face="Times New Roman">if(x > a[middle]) left = middle + 1;<o:p></o:p></FONT></P>
, W6 E9 [: S2 d9 O) f; T" l<P ><FONT face="Times New Roman"> else right = middle - 1;<o:p></o:p></FONT></P>
' x' m! l4 V9 h0 r% [<P >(<FONT face="Times New Roman">2</FONT>)算法<FONT face="Times New Roman">2</FONT>不正确。<o:p></o:p></P>
6 d. o5 [# J+ c2 h4 G- ?
<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>
4 Q) W5 l9 [9 L+ Y0 K' t<P >另外,当<FONT face="Times New Roman">n=0</FONT>时执行<FONT face="Times New Roman">if(x == a
)...</FONT>时将出现下标越界错误。<o:p></o:p></P>
- o0 M( C/ \% U5 n
<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>
e8 v2 ]8 @- y! J$ h7 W' d
<P >(<FONT face="Times New Roman">3</FONT>)算法<FONT face="Times New Roman">3</FONT>不正确。<o:p></o:p></P>
0 W) ?4 ~. F/ J9 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>
; C; K! ?4 ?& F! g* N<P >原因:与算法<FONT face="Times New Roman">2</FONT>相同。<o:p></o:p></P>
9 \: J3 g2 Y9 a
<P >(<FONT face="Times New Roman">4</FONT>)算法<FONT face="Times New Roman">4</FONT>不正确。<o:p></o:p></P>
3 X( P+ Q3 C ~3 k0 k9 }" O
<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>
% x% g( h0 j+ y, T
<P >原因:循环条件和对变量<FONT face="Times New Roman">left</FONT>值的修改有错误。<o:p></o:p></P>
/ p( e- U& X, @1 x; h% E<P >(<FONT face="Times New Roman">5</FONT>)此算法正确。<o:p></o:p></P>
( v! h( f! l A, r
<P >证明:当<FONT face="Times New Roman">n=0</FONT>或<FONT face="Times New Roman">n=1</FONT>时,算法显然正确。<o:p></o:p></P>
/ Z: M/ u: g; 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>
. f5 X- B% F! M4 y# J- t
<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>
, A. v* H# _" y ^( j
<P >即:middle > left成立。<o:p></o:p></P>
( u: K3 C3 {/ F) V: p7 E
<P >且<FONT face="Times New Roman">middle = (left + right + 1) / 2 = [(left + 1) + right] / 2 </FONT>≤ 2right / 2 = right,<o:p></o:p></P>
3 d( _3 b7 `9 v! r2 t<P >∴left < middle ≤ right恒成立。<o:p></o:p></P>
( U6 C& M2 A7 k$ U4 [0 T4 f<P >因此,每次循环之后,right与left之差必然减小,在有限次循环后,必有left = right条件成立,从而循环结束。<o:p></o:p></P>
* s9 k8 r& Y6 `5 i/ O8 h
<P >如果x值与数组a的某个元素值相等,则在循环结束时显然有x = a
且x = a
成立,否则x ≠a
,即未找到x,<o:p></o:p></P>
. e3 a: G L3 B, i% Y' D* ]& k2 B; Z<P >∴返回结果正确。<o:p></o:p></P>
W+ x3 I! Y8 s7 o7 ?/ |, G
<P >(6)算法6是错误的。<o:p></o:p></P>
, ~5 V( r9 K9 `' T8 g" Y3 @4 f9 `& z
<P >当执行到某次循环x = a[middle]成立时,再执行if 语句中的<o:p></o:p></P>
! W0 }: V4 F7 j! p3 y3 r3 S+ G8 n<P >left = middle + 1;<o:p></o:p></P>
+ j$ S) T% Y* A A9 w<P >就把结果丢失了,导致错误。而且还可能会导致下标越界错误。例如:<o:p></o:p></P>
. j' }$ @" U. [" Z, U0 ^
<P >当n = 2且x = a[1]时即会出现这些情况。<o:p></o:p></P>
O% p/ F" T$ b6 l7 a<P >原因:if 语句中的left = middle + 1;应改为left = middle;<o:p></o:p></P>
0 z: j: p* l" k3 W/ E<P >(7)算法7是错误的。<o:p></o:p></P>
9 s8 m* a5 [: n8 i2 U( |
<P >在循环过程中,一旦出现<o:p></o:p></P>
# ~# [4 u: i# a7 m) `/ k' }# L6 a<P >a
≤ x < a[left + 1],则必进入死循环。<o:p></o:p></P>
# a: _% B3 F( w# R( [2 q" a' |<P >原因:right值的修改不正确。<o:p></o:p></P>