QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2837|回复: 0
打印 上一主题 下一主题

[讨论]一道分治法的题目,仅供参考

[复制链接]
字体大小: 正常 放大
lllaaa        

2

主题

2

听众

27

积分

升级  23.16%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-9-29 19:02 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
< ><FONT face="Times New Roman">2-2</FONT>、下面的<FONT face="Times New Roman">7</FONT>个算法与本章中的二分搜索算法<FONT face="Times New Roman">binarySearch</FONT>略有不同。请判断这<FONT face="Times New Roman">7</FONT>个算法的正确性。如果算法不正确,请说明错误产生的原因。如果算法正确,请给出算法的正确性证明。</P>
6 L5 C, v. K! I' n* o8 B' r* O< ><FONT face="Times New Roman">public static int binarySearch1(int[] a, int x, int n)</FONT></P>
* J! S+ v  G( M; Q! u# ~< ><FONT face="Times New Roman">{</FONT></P>
- s; v! J  `' |, r< ><FONT face="Times New Roman">              int left = 0, right = n-1;</FONT></P>/ C- e2 o: e0 b; k$ `; @2 X" _
< ><FONT face="Times New Roman">              while(left &lt;= right)</FONT></P>
! {0 ^  x6 s5 b' E! W  f. m< ><FONT face="Times New Roman">{</FONT></P>
4 T1 V; I- N- d! _, z) N< ><FONT face="Times New Roman">   int middle = (left + right) / 2;</FONT></P>$ t0 ^' N' p) H) I  `6 _
< ><FONT face="Times New Roman">   if(x == a[middle]) return middle;</FONT></P>
6 a- c: ^/ ?3 r) Y1 L2 g2 S< ><FONT face="Times New Roman">   if(x &gt; a[middle]) left = middle;</FONT></P>: A7 b; Q! U' v# o9 E
< ><FONT face="Times New Roman">   else right = middle;</FONT></P>
8 u" A! G8 i7 }) F9 l6 f2 i< ><FONT face="Times New Roman">}//while</FONT></P>
: ]* U9 X4 E5 E% Y< ><FONT face="Times New Roman">return –1;</FONT></P>
, O; q  q9 U* E< ><FONT face="Times New Roman">}</FONT></P>
4 i' n: B3 c' G# H. X$ h7 z; C( W< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>+ l+ x* ?: P  P/ [( O: z+ N; P
< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
  ]+ B/ K) H* j8 ]3 m5 q. p< ><FONT face="Times New Roman">public static int binarySearch2(int[] a, int x, int n)</FONT></P>9 S0 o7 e' {) P) q! i, ^7 r
< ><FONT face="Times New Roman">{</FONT></P>$ R( z; T9 e, F; H; r) R
< ><FONT face="Times New Roman">              int left = 0, right = n-1;</FONT></P>
( m$ {0 K" c& Z5 r8 p' N< ><FONT face="Times New Roman">              while(left &lt; right-1)</FONT></P>
' b1 v. X. ], o8 ^- d< ><FONT face="Times New Roman">{</FONT></P>/ i- }2 }# s" ^2 w+ G
< ><FONT face="Times New Roman">   int middle = (left + right) / 2;</FONT></P>8 P- F6 t6 V5 y7 }. X
< ><FONT face="Times New Roman">   if(x &lt; a[middle]) right = middle;</FONT></P>) {. h2 q, n3 `+ F. c6 b# p$ G
< ><FONT face="Times New Roman">   else left = middle;</FONT></P>
( h3 O( W+ ~, ?! |# s6 H< ><FONT face="Times New Roman">}//while</FONT></P>
, Z3 E4 ]$ F, s$ `1 W) E< ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>. ~6 b4 K( c& J+ X! W
< ><FONT face="Times New Roman">else return –1;</FONT></P>
5 d9 z* d7 {' n/ x0 z! R< ><FONT face="Times New Roman">}</FONT></P>$ n$ b1 _, ]$ o! Y$ D
< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>8 n2 i; z3 _' }9 H
< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>5 a: t1 l% x4 p+ S
< ><FONT face="Times New Roman">public static int binarySearch3(int[] a, int x, int n)</FONT></P>
2 o$ O$ Y5 S7 P4 c$ b" A. P5 t<P ><FONT face="Times New Roman">{</FONT></P>
6 p0 a7 c) k1 L% z( `# G/ F<P ><FONT face="Times New Roman">              int left = 0, right = n-1;</FONT></P>
0 U# l+ z1 t. P* X<P ><FONT face="Times New Roman">              while(left+1 != right)</FONT></P>
  s! @5 e+ R' |; v: w. D! v0 T: q<P ><FONT face="Times New Roman">{</FONT></P>% l: B5 y* C: _9 s+ n& p' ?
<P ><FONT face="Times New Roman">   int middle = (left + right) / 2;</FONT></P>2 ?3 \) {- `& u2 f$ c, s4 S
<P ><FONT face="Times New Roman">   if(x &gt;= a[middle]) left = middle;</FONT></P>7 S: T% |6 V& o* Y4 X
<P ><FONT face="Times New Roman">   else right = middle;</FONT></P>4 M9 [9 k8 o) s( Z8 I/ f
<P ><FONT face="Times New Roman">}//while</FONT></P>* ^! \! R! I/ c9 |1 I
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
" B- _* U& g  k" ~' E<P ><FONT face="Times New Roman">else return –1;</FONT></P>  b1 s) o) j) x* D. G! u
<P ><FONT face="Times New Roman">}</FONT></P>4 p: |% w( b2 l5 x) s* \+ D- g
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>; }9 m1 I- Q# ?; D& M9 x7 u2 a
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>3 K+ h" E- s+ ?, l, v
<P ><FONT face="Times New Roman">public static int binarySearch4(int[] a, int x, int n)</FONT></P>
; C% U; D, M4 R; s$ f7 Q% j<P ><FONT face="Times New Roman">{</FONT></P>9 v8 S' E% ~- [" ]
<P ><FONT face="Times New Roman">              if(n &gt; 0 &amp;&amp; x &gt;= a[0])</FONT></P>' J: D9 A$ u% M9 D% Q4 U
<P ><FONT face="Times New Roman">              {</FONT></P>& |2 P( P0 W: y6 Y
<P ><FONT face="Times New Roman">                     int left = 0, right = n-1;</FONT></P>
& j) O. A/ \5 _2 I+ p+ }<P ><FONT face="Times New Roman">              while(left &lt; right)</FONT></P>; ^! D9 k; T$ L: `' X4 ?
<P ><FONT face="Times New Roman">              {</FONT></P>
( v# u8 ^- k5 \* }, t6 o<P ><FONT face="Times New Roman">          int middle = (left + right) / 2;</FONT></P>
9 s4 l, {5 L+ k# ^2 C9 N" q<P ><FONT face="Times New Roman">if(x &lt; a[middle]) right = middle - 1;</FONT></P>
6 @  @$ w3 b/ ]1 q3 s, R9 ^+ i& T/ \6 [<P ><FONT face="Times New Roman">else left = middle;</FONT></P>7 t% U/ }' J1 k# _
<P ><FONT face="Times New Roman">}//while</FONT></P>/ K% y$ k) X  a. q, C( @
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>( p2 g! l6 V1 D9 @
<P ><FONT face="Times New Roman">}//if</FONT></P>
( D, f. ~3 k6 H<P ><FONT face="Times New Roman">return –1;</FONT></P>
) q" w$ {+ b0 V2 n- T0 G- b1 K# \<P ><FONT face="Times New Roman">}</FONT></P>8 n- m; Q( p0 r
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
- P" D, R. S1 ?7 y0 {<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>& x; u2 S8 `1 |' ^' E! w0 N& O
<P ><FONT face="Times New Roman">public static int binarySearch5(int[] a, int x, int n)</FONT></P>
& B$ q1 N! V, p# E<P ><FONT face="Times New Roman">{</FONT></P># [; X1 s; t; ]
<P ><FONT face="Times New Roman">              if(n &gt; 0 &amp;&amp; x &gt;= a[0])</FONT></P>
3 F* W. R" b) ?! n<P ><FONT face="Times New Roman">              {</FONT></P>. j9 A) x3 A, `4 G. q6 [
<P ><FONT face="Times New Roman">                     int left = 0, right = n-1;</FONT></P>
' b% v/ e( `/ F0 t<P ><FONT face="Times New Roman">              while(left &lt; right)</FONT></P>* d/ W8 }! Y% w, c  w5 m
<P ><FONT face="Times New Roman">              {</FONT></P>
. c. G' m; Q8 g<P ><FONT face="Times New Roman">          int middle = (left + right + 1) / 2;</FONT></P>
' E2 j- \5 _+ P* z2 Z( }8 T2 w<P ><FONT face="Times New Roman">if(x &lt; a[middle]) right = middle - 1;</FONT></P>5 @8 ^' z$ n9 H* F; P: ^
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
* q2 x" E, s" T+ ]$ S, s<P ><FONT face="Times New Roman">}//while</FONT></P>& m$ @: i& G/ m4 e2 H# |
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>+ w: H# x; l0 Y% u% f  W
<P ><FONT face="Times New Roman">}//if</FONT></P>
! |; L1 e; Y6 o' }3 R! j% x<P ><FONT face="Times New Roman">return –1;</FONT></P>/ }' ?5 a7 E* p+ m
<P ><FONT face="Times New Roman">}</FONT></P>
% }9 h& {* s5 q& d  Q<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>' ^- [6 z; @) r4 ~  g2 {& t
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>- v9 F' U3 L- S7 B
<P ><FONT face="Times New Roman">public static int binarySearch6(int[] a, int x, int n)</FONT></P>
9 D0 P0 O/ g2 G1 @" j( I<P ><FONT face="Times New Roman">{</FONT></P>! j" R3 y6 ~. [4 G) e2 V$ D: {
<P ><FONT face="Times New Roman">              if(n &gt; 0 &amp;&amp; x &gt;= a[0])</FONT></P>3 w' f- y, P8 |9 T9 C/ ~7 e% x
<P ><FONT face="Times New Roman">              {</FONT></P>, Z( O# g+ p4 ^: L
<P ><FONT face="Times New Roman">                     int left = 0, right = n-1;</FONT></P>
- ]% n) v$ m8 v( q  v6 x<P ><FONT face="Times New Roman">              while(left &lt; right)</FONT></P>$ Z. V% d' N- ^" B
<P ><FONT face="Times New Roman">              {</FONT></P>  ?4 ]* C5 I( k+ Z; J5 ~
<P ><FONT face="Times New Roman">          int middle = (left + right + 1) / 2;</FONT></P>+ K& i. a# x1 F, P
<P ><FONT face="Times New Roman">if(x &lt; a[middle]) right = middle - 1;</FONT></P>
# H6 u0 E& n6 p% `- \<P ><FONT face="Times New Roman">else left = middle + 1;</FONT></P>$ [; [: ^8 ^8 K6 W+ m
<P ><FONT face="Times New Roman">}//while</FONT></P>1 C8 m% E9 Q$ K% V% V
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>/ V! ^$ [$ D" t. ]
<P ><FONT face="Times New Roman">}//if</FONT></P>
* X7 h/ w% \/ m, p4 A<P ><FONT face="Times New Roman">return –1;</FONT></P>
. U4 E; s' }) F3 q<P ><FONT face="Times New Roman">}</FONT></P>
; z7 Z# n/ Z$ h<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
3 m0 B; R4 f! \  j7 \7 z1 a<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
9 T  ^6 G% r0 i6 E" L% p<P ><FONT face="Times New Roman">public static int binarySearch7(int[] a, int x, int n)</FONT></P>, L2 [; J9 p5 N" ^' g# n0 Y
<P ><FONT face="Times New Roman">{</FONT></P>) \0 l0 H* c, w$ S" A
<P ><FONT face="Times New Roman">              if(n &gt; 0 &amp;&amp; x &gt;= a[0])</FONT></P>! _5 [. t& A3 G8 i5 Q- B
<P ><FONT face="Times New Roman">              {</FONT></P>9 _' z5 M' l7 F2 }  M& B
<P ><FONT face="Times New Roman">                     int left = 0, right = n-1;</FONT></P>
8 m9 z$ `# W/ k4 }- P% C<P ><FONT face="Times New Roman">              while(left &lt; right)</FONT></P>) D+ k* L6 ?+ _+ r
<P ><FONT face="Times New Roman">              {</FONT></P>5 c! V6 @3 o1 c4 o0 S8 l2 v2 ]
<P ><FONT face="Times New Roman">          int middle = (left + right +1) / 2;</FONT></P>5 p( w+ G) K5 S# h* r
<P ><FONT face="Times New Roman">if(x &lt; a[middle]) right = middle;</FONT></P>
' u  e  g1 w" i  N7 }5 _<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
) q0 J, S! ~# R8 ^0 E+ e" h9 u: q) {, }<P ><FONT face="Times New Roman">}//while</FONT></P>
' W- d% D. h0 L; j<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>7 n9 ^9 f. Y- V* {9 s
<P ><FONT face="Times New Roman">}//if</FONT></P>+ T4 v  U3 x3 G4 p
<P ><FONT face="Times New Roman">return –1;</FONT></P>' G1 {# v6 w8 b+ [9 ?
<P ><FONT face="Times New Roman">}</FONT></P>
$ w% `' K2 n- k% H- ~3 V* D0 J- X<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
3 w# Y7 e: f( T; U5 e3 |! @<P >解:(<FONT face="Times New Roman">1</FONT>)算法<FONT face="Times New Roman">1</FONT>不正确。<o:p></o:p></P>- O2 [# [" `, v* P, p9 Y8 a
<P >当在数组<FONT face="Times New Roman">a</FONT>中找不到与<FONT face="Times New Roman">x</FONT>相等的元素时,算法将进入死循环状态。<o:p></o:p></P>
* \9 c& S2 ^7 t9 `$ A4 [<P >原因:每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改不正确。应修改如下:<o:p></o:p></P>3 @* M/ r1 ]) J6 m6 X7 W. y
<P ><FONT face="Times New Roman">if(x &gt; a[middle]) left = middle + 1;<o:p></o:p></FONT></P>
- w% A' d3 m7 d+ T- x3 o<P ><FONT face="Times New Roman">       else right = middle - 1;<o:p></o:p></FONT></P>2 N0 e4 o! _2 v% T% `
<P >(<FONT face="Times New Roman">2</FONT>)算法<FONT face="Times New Roman">2</FONT>不正确。<o:p></o:p></P>
' Q  c1 V9 D; ?1 F9 ^<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>
2 ~. z  o. U2 ~1 q& K; q, k9 f<P >另外,当<FONT face="Times New Roman">n=0</FONT>时执行<FONT face="Times New Roman">if(x == a
)...</FONT>时将出现下标越界错误。<o:p></o:p></P>
9 X5 K( r  o( x' K; D5 a& Q4 n% W<P >原因:循环结束条件错误,应改为<FONT face="Times New Roman">left &lt;= right</FONT>。每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改也不正确。<o:p></o:p></P>) f/ {+ G- B* H5 T5 u) @0 @  ?
<P >(<FONT face="Times New Roman">3</FONT>)算法<FONT face="Times New Roman">3</FONT>不正确。<o:p></o:p></P>. U% }8 a1 h" a) F( m
<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 I- k4 B$ g# l# N2 z+ o9 V: V! E<P >原因:与算法<FONT face="Times New Roman">2</FONT>相同。<o:p></o:p></P>
- g. Q, q/ q4 A1 ?1 I3 [' B<P >(<FONT face="Times New Roman">4</FONT>)算法<FONT face="Times New Roman">4</FONT>不正确。<o:p></o:p></P>
" T4 [5 C8 ~/ Q: g+ |6 U3 U& A<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>
! ~  K/ ?8 ^1 k3 I. J- s<P >原因:循环条件和对变量<FONT face="Times New Roman">left</FONT>值的修改有错误。<o:p></o:p></P>' z( w) t0 a) G. ?8 H
<P >(<FONT face="Times New Roman">5</FONT>)此算法正确。<o:p></o:p></P>. C2 s8 t8 Y& H
<P >证明:当<FONT face="Times New Roman">n=0</FONT>或<FONT face="Times New Roman">n=1</FONT>时,算法显然正确。<o:p></o:p></P>
, t4 s9 v# j9 ]5 W<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 &lt; right,<o:p></o:p></P>
0 @8 M0 A! q+ d$ t! b<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>
8 ^8 z% e3 D6 [' T<P >即:middle &gt; left成立。<o:p></o:p></P>
4 j  I+ o, |" u; @<P >且<FONT face="Times New Roman">middle = (left + right + 1) / 2 = [(left + 1) + right] / 2 </FONT>≤ 2right / 2 = right,<o:p></o:p></P>7 n3 l( k5 o1 H# [7 ^
<P >∴left &lt; middle ≤ right恒成立。<o:p></o:p></P>" z6 N1 ~& T6 K4 }, z$ d- ^& ?2 L
<P >因此,每次循环之后,right与left之差必然减小,在有限次循环后,必有left = right条件成立,从而循环结束。<o:p></o:p></P>
/ o9 ^0 d7 n6 K" _2 ~& f, c<P >如果x值与数组a的某个元素值相等,则在循环结束时显然有x = a
且x = a
成立,否则x ≠a
,即未找到x,<o:p></o:p></P>1 A# T: u9 ]) t
<P >∴返回结果正确。<o:p></o:p></P>8 i( |& l6 Q# G+ i+ T" Y0 t
<P >(6)算法6是错误的。<o:p></o:p></P>
7 k1 x4 g8 o7 L" Q<P >当执行到某次循环x = a[middle]成立时,再执行if 语句中的<o:p></o:p></P>8 ~* R2 S- t& n3 R
<P >left = middle + 1;<o:p></o:p></P>
7 |2 k, [' B* @# u, q& o# b<P >就把结果丢失了,导致错误。而且还可能会导致下标越界错误。例如:<o:p></o:p></P>! J1 s, n; T+ N
<P >当n = 2且x = a[1]时即会出现这些情况。<o:p></o:p></P>
" t  k& d! V% }  F% d% }<P >原因:if 语句中的left = middle + 1;应改为left = middle;<o:p></o:p></P>% Z$ {1 U# m2 K+ a" B7 A
<P >(7)算法7是错误的。<o:p></o:p></P>+ T: h; g, \' J4 Y5 ~
<P >在循环过程中,一旦出现<o:p></o:p></P>3 H& `1 z! a/ m! T9 C/ N
<P >a
≤ x &lt; a[left + 1],则必进入死循环。<o:p></o:p></P>1 S% h! o$ @2 h" |# O7 X; t6 |
<P >原因:right值的修改不正确。<o:p></o:p></P>
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-7-21 14:32 , Processed in 0.828901 second(s), 51 queries .

回顶部