QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2836|回复: 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>
' b7 x' K7 K3 e( P: g) T< ><FONT face="Times New Roman">public static int binarySearch1(int[] a, int x, int n)</FONT></P>
  ^# R5 W. U: N  L6 j. j< ><FONT face="Times New Roman">{</FONT></P>' ~- ?( t$ ?4 R+ A1 ^
< ><FONT face="Times New Roman">              int left = 0, right = n-1;</FONT></P>
; w2 ^6 j* e8 R- G# R' `( x! Z8 |< ><FONT face="Times New Roman">              while(left &lt;= right)</FONT></P>
/ ^& N6 R( X5 R# I! p4 j< ><FONT face="Times New Roman">{</FONT></P>! N" p( G2 V* d
< ><FONT face="Times New Roman">   int middle = (left + right) / 2;</FONT></P>
0 u( p' W4 K% a0 x9 i& k< ><FONT face="Times New Roman">   if(x == a[middle]) return middle;</FONT></P>  S, r* W9 r  I* q: F9 Q- Z8 {
< ><FONT face="Times New Roman">   if(x &gt; a[middle]) left = middle;</FONT></P>
) n% Y3 @* ~9 t" h( `< ><FONT face="Times New Roman">   else right = middle;</FONT></P>0 y" P0 M: p- m
< ><FONT face="Times New Roman">}//while</FONT></P>/ m0 j$ @' z- L
< ><FONT face="Times New Roman">return –1;</FONT></P>
) D- ?  \% _7 ~7 n< ><FONT face="Times New Roman">}</FONT></P>
+ W) G3 m- U- g/ k5 N, m/ Z0 ?< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>: w! C, g. p1 [2 o. S: }# L) G: K
< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>' X+ k2 K% Z* p+ p1 E( U. z  F5 A3 V3 |
< ><FONT face="Times New Roman">public static int binarySearch2(int[] a, int x, int n)</FONT></P>% f) `( n& R8 e" Y" ?9 I/ \, [
< ><FONT face="Times New Roman">{</FONT></P>
# m' G; W' J9 T! r% Q< ><FONT face="Times New Roman">              int left = 0, right = n-1;</FONT></P>; ~' E+ \. f4 T# v% m) a
< ><FONT face="Times New Roman">              while(left &lt; right-1)</FONT></P>
% [! v5 f: U& j. v- O7 u- E0 n$ b< ><FONT face="Times New Roman">{</FONT></P>
6 g: \( q+ l8 d2 W- h: y3 N< ><FONT face="Times New Roman">   int middle = (left + right) / 2;</FONT></P>
+ m" u) {  i6 {  Z< ><FONT face="Times New Roman">   if(x &lt; a[middle]) right = middle;</FONT></P>
" ~. X  N( U5 e< ><FONT face="Times New Roman">   else left = middle;</FONT></P>8 l8 l' l2 {) @  D$ n6 e% w" K
< ><FONT face="Times New Roman">}//while</FONT></P>. u6 Q* v& f8 G" h4 z+ w4 S- b+ A' _
< ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
; s; X& X; A! A< ><FONT face="Times New Roman">else return –1;</FONT></P>: C- v/ A/ W, R' H
< ><FONT face="Times New Roman">}</FONT></P>
  e8 X% r) {* ~, s< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>* b( z% M8 ?% p- j7 F2 A5 }
< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>! `; \8 o8 P, }" S5 z
< ><FONT face="Times New Roman">public static int binarySearch3(int[] a, int x, int n)</FONT></P>
" }3 e3 A' U4 U$ G; X<P ><FONT face="Times New Roman">{</FONT></P>' t' d# E& j0 A$ O  w0 Q" k3 S* h
<P ><FONT face="Times New Roman">              int left = 0, right = n-1;</FONT></P>
/ ~2 Z6 z" \3 k# u% L<P ><FONT face="Times New Roman">              while(left+1 != right)</FONT></P>1 \, z& c2 @6 ]" ~- B, t
<P ><FONT face="Times New Roman">{</FONT></P>
) ^6 X) p# Q3 c0 A<P ><FONT face="Times New Roman">   int middle = (left + right) / 2;</FONT></P>+ W+ b$ o2 i: D% d
<P ><FONT face="Times New Roman">   if(x &gt;= a[middle]) left = middle;</FONT></P>+ p6 R4 _' e8 n( J3 f7 m8 Y+ a. e: O3 M0 m4 j
<P ><FONT face="Times New Roman">   else right = middle;</FONT></P>  c6 Q4 [+ x' n) Q$ g$ E7 ?0 X; j( a
<P ><FONT face="Times New Roman">}//while</FONT></P>, e5 x' C; L/ J5 G+ p
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>1 G. ~4 P9 Y, V# c) i0 E
<P ><FONT face="Times New Roman">else return –1;</FONT></P>7 a+ Y* L9 D' |) `
<P ><FONT face="Times New Roman">}</FONT></P>
1 Y) N- t+ g6 a# `' K5 h) n<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
5 K- w, p* n; X3 S( ?<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>1 S: d; W  M+ A, n  O+ h
<P ><FONT face="Times New Roman">public static int binarySearch4(int[] a, int x, int n)</FONT></P>5 E% y! M2 s& W2 H  e
<P ><FONT face="Times New Roman">{</FONT></P>
' _  V9 p: U: r' O<P ><FONT face="Times New Roman">              if(n &gt; 0 &amp;&amp; x &gt;= a[0])</FONT></P>
' f0 D$ y1 {+ V' m<P ><FONT face="Times New Roman">              {</FONT></P>
1 x6 C( W5 ^* E1 k) E<P ><FONT face="Times New Roman">                     int left = 0, right = n-1;</FONT></P>
! D0 j( q7 {- K$ E( o9 s( J! H<P ><FONT face="Times New Roman">              while(left &lt; right)</FONT></P>
- N& A3 \9 M. v  q; {6 k, `<P ><FONT face="Times New Roman">              {</FONT></P>
4 {) g' A$ ~9 h<P ><FONT face="Times New Roman">          int middle = (left + right) / 2;</FONT></P>, I5 d9 G" |5 Q# D0 q, z8 ~
<P ><FONT face="Times New Roman">if(x &lt; a[middle]) right = middle - 1;</FONT></P>
1 w  g" T6 ?% h. r$ j, E: {<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
1 {$ `$ U5 R. K6 z, B<P ><FONT face="Times New Roman">}//while</FONT></P>
2 o9 H. a0 O+ \  `- \& u<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
2 p' p  s+ N$ x& w<P ><FONT face="Times New Roman">}//if</FONT></P>+ a; D. |, S0 G* L, u5 b) g
<P ><FONT face="Times New Roman">return –1;</FONT></P>. d* a& A  {9 \; K2 b2 ^* I4 q- g
<P ><FONT face="Times New Roman">}</FONT></P>1 B. i! q! e7 e* L' m' Q
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
7 q! |- k8 x; D<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>4 x1 }% X9 T# ]8 n  R
<P ><FONT face="Times New Roman">public static int binarySearch5(int[] a, int x, int n)</FONT></P>
% ]7 t: M6 v8 e<P ><FONT face="Times New Roman">{</FONT></P>
# p4 E/ \3 n; S; @# h& T4 K7 g<P ><FONT face="Times New Roman">              if(n &gt; 0 &amp;&amp; x &gt;= a[0])</FONT></P>  _0 P! u) Y7 J6 q7 U
<P ><FONT face="Times New Roman">              {</FONT></P>
5 p* M9 x  y: }* M, M* r<P ><FONT face="Times New Roman">                     int left = 0, right = n-1;</FONT></P>) H3 `3 v2 X: _* r; `% z
<P ><FONT face="Times New Roman">              while(left &lt; right)</FONT></P>
% J$ d* Q! V$ o3 [& x! [<P ><FONT face="Times New Roman">              {</FONT></P>
6 R  ^* ?% y' D0 U/ p7 _. D& f! ]<P ><FONT face="Times New Roman">          int middle = (left + right + 1) / 2;</FONT></P>, l. P& O0 c% }3 `! ?' ~
<P ><FONT face="Times New Roman">if(x &lt; a[middle]) right = middle - 1;</FONT></P>9 d; q$ t0 Y$ m9 M3 @# b1 @
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
6 u) _$ d# R9 f" W7 m<P ><FONT face="Times New Roman">}//while</FONT></P>
; j9 f9 E, L; I# M8 i$ |<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>/ g3 y" S- q0 s: ?8 L2 g2 c$ {
<P ><FONT face="Times New Roman">}//if</FONT></P>6 p4 T* Y4 t( ~. @9 s8 `
<P ><FONT face="Times New Roman">return –1;</FONT></P>
5 T8 M" \" S8 R' @$ }<P ><FONT face="Times New Roman">}</FONT></P>0 z+ @1 ?% v7 \/ q6 Q% e0 |  }
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>8 `! @( v4 q/ I+ y& X! d
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>4 f1 I  i) q- _7 [: V
<P ><FONT face="Times New Roman">public static int binarySearch6(int[] a, int x, int n)</FONT></P>6 b/ P/ H' x  \8 F
<P ><FONT face="Times New Roman">{</FONT></P>
: Q8 |( o: f; H2 Z) |2 L<P ><FONT face="Times New Roman">              if(n &gt; 0 &amp;&amp; x &gt;= a[0])</FONT></P>
8 N' h; Y3 K; k<P ><FONT face="Times New Roman">              {</FONT></P>( q2 R$ V% J: w3 g" S, h
<P ><FONT face="Times New Roman">                     int left = 0, right = n-1;</FONT></P>
8 D; z8 v' p5 N, B6 s, y<P ><FONT face="Times New Roman">              while(left &lt; right)</FONT></P>1 `$ _" v+ [- O' B: ^* W
<P ><FONT face="Times New Roman">              {</FONT></P>
- Y6 c4 x2 j5 w3 n2 n' j# g<P ><FONT face="Times New Roman">          int middle = (left + right + 1) / 2;</FONT></P>
9 Y. @6 F: v' e: P9 n0 ]& @<P ><FONT face="Times New Roman">if(x &lt; a[middle]) right = middle - 1;</FONT></P>  X% m# `& a/ }7 r( w$ l
<P ><FONT face="Times New Roman">else left = middle + 1;</FONT></P>
- r0 m1 C$ m) F( n1 T" N<P ><FONT face="Times New Roman">}//while</FONT></P>/ y. q2 B! i: a) M
<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
& J5 P# o( s0 S<P ><FONT face="Times New Roman">}//if</FONT></P>
6 O) G: z6 X! ?<P ><FONT face="Times New Roman">return –1;</FONT></P>0 D; X' m4 x: p# ?3 V
<P ><FONT face="Times New Roman">}</FONT></P>4 Q+ I$ Y0 @% q' X* _8 ]9 o
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
3 X& Z! a! o: X& E- n& S<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>- w* _+ H; I* T2 u
<P ><FONT face="Times New Roman">public static int binarySearch7(int[] a, int x, int n)</FONT></P>: A. ~8 ~0 O& ]! I
<P ><FONT face="Times New Roman">{</FONT></P>. @! U' P% w  h% x
<P ><FONT face="Times New Roman">              if(n &gt; 0 &amp;&amp; x &gt;= a[0])</FONT></P>8 v# X+ g9 }3 l# }/ ]" f
<P ><FONT face="Times New Roman">              {</FONT></P>. X3 m% y) R: C! [  o
<P ><FONT face="Times New Roman">                     int left = 0, right = n-1;</FONT></P>
+ V' j( p  ~8 P% [<P ><FONT face="Times New Roman">              while(left &lt; right)</FONT></P>" }0 I7 K3 j5 \& n' F
<P ><FONT face="Times New Roman">              {</FONT></P>
6 L# F3 K" l) q; h<P ><FONT face="Times New Roman">          int middle = (left + right +1) / 2;</FONT></P>
/ J5 F5 q; Y: ?5 u<P ><FONT face="Times New Roman">if(x &lt; a[middle]) right = middle;</FONT></P>( B% h/ R* _9 `
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
' E1 j6 R, m( [% _$ W3 r<P ><FONT face="Times New Roman">}//while</FONT></P>
3 x6 f* ~- v/ w& F' z8 x+ |' k2 i<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
8 \8 e8 @* N0 t9 I<P ><FONT face="Times New Roman">}//if</FONT></P>
5 ^& ~" p2 L( D/ r<P ><FONT face="Times New Roman">return –1;</FONT></P>
' X% y9 D( a$ H<P ><FONT face="Times New Roman">}</FONT></P>; U2 v3 Z# k. |1 T: t7 T
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>* X7 ~$ I2 L( H3 |" [# H8 d
<P >解:(<FONT face="Times New Roman">1</FONT>)算法<FONT face="Times New Roman">1</FONT>不正确。<o:p></o:p></P>0 P9 Q! E) y9 x; o, b
<P >当在数组<FONT face="Times New Roman">a</FONT>中找不到与<FONT face="Times New Roman">x</FONT>相等的元素时,算法将进入死循环状态。<o:p></o:p></P>+ [8 l9 h  U. {/ F1 |: t
<P >原因:每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改不正确。应修改如下:<o:p></o:p></P>& ?% O: D: s0 S
<P ><FONT face="Times New Roman">if(x &gt; a[middle]) left = middle + 1;<o:p></o:p></FONT></P>
4 `- d" ?: B1 Z7 @, k& Q/ m<P ><FONT face="Times New Roman">       else right = middle - 1;<o:p></o:p></FONT></P>
8 z4 O% x" @1 t) v  ?8 H<P >(<FONT face="Times New Roman">2</FONT>)算法<FONT face="Times New Roman">2</FONT>不正确。<o:p></o:p></P>
2 {. n! [) B; E( p0 L1 S8 P<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>
0 n: s, j; Y+ O/ r' j5 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 a0 g4 o5 N  M! }5 N2 v<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># M7 A6 \6 r# A2 _5 r7 I. {
<P >(<FONT face="Times New Roman">3</FONT>)算法<FONT face="Times New Roman">3</FONT>不正确。<o:p></o:p></P>
" g7 B$ D1 }! `: A$ [<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>! \4 o( t3 z: e5 v2 X  M
<P >原因:与算法<FONT face="Times New Roman">2</FONT>相同。<o:p></o:p></P>
5 B# K$ {' u2 a' g; I7 E: T<P >(<FONT face="Times New Roman">4</FONT>)算法<FONT face="Times New Roman">4</FONT>不正确。<o:p></o:p></P>% D; R7 s9 ~- ]( E* M' a0 x0 I
<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>
2 r; m) B0 H5 \. l7 P7 d<P >原因:循环条件和对变量<FONT face="Times New Roman">left</FONT>值的修改有错误。<o:p></o:p></P>
, f# m, H2 Q7 h  {<P >(<FONT face="Times New Roman">5</FONT>)此算法正确。<o:p></o:p></P>
- G: r4 [2 V9 r  T/ b. w( Z1 C<P >证明:当<FONT face="Times New Roman">n=0</FONT>或<FONT face="Times New Roman">n=1</FONT>时,算法显然正确。<o:p></o:p></P>
: w( M; x& I+ }8 Y<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>
; Z5 q" X2 V) F0 E4 }: T- i<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>1 `9 g5 b/ F) }
<P >即:middle &gt; left成立。<o:p></o:p></P>) d! r  h. w/ d5 m1 V7 P( x
<P >且<FONT face="Times New Roman">middle = (left + right + 1) / 2 = [(left + 1) + right] / 2 </FONT>≤ 2right / 2 = right,<o:p></o:p></P>+ w7 Y0 e* n/ M: f, s' t
<P >∴left &lt; middle ≤ right恒成立。<o:p></o:p></P># \6 c  {. I; A
<P >因此,每次循环之后,right与left之差必然减小,在有限次循环后,必有left = right条件成立,从而循环结束。<o:p></o:p></P>
: ?. J# {/ V2 m$ x4 r<P >如果x值与数组a的某个元素值相等,则在循环结束时显然有x = a
且x = a
成立,否则x ≠a
,即未找到x,<o:p></o:p></P>
  B9 j/ H4 i& o8 b2 a<P >∴返回结果正确。<o:p></o:p></P>
' u6 R6 G. Y, E. s4 s2 u<P >(6)算法6是错误的。<o:p></o:p></P>6 [5 F! T# a8 c- Y
<P >当执行到某次循环x = a[middle]成立时,再执行if 语句中的<o:p></o:p></P>8 z$ R# E$ V9 u
<P >left = middle + 1;<o:p></o:p></P>% C; Z- y8 q/ N/ ?% e6 ~! R( E8 g
<P >就把结果丢失了,导致错误。而且还可能会导致下标越界错误。例如:<o:p></o:p></P>
4 u' E: W1 C0 N+ X$ N<P >当n = 2且x = a[1]时即会出现这些情况。<o:p></o:p></P>5 k8 q: U) m# s
<P >原因:if 语句中的left = middle + 1;应改为left = middle;<o:p></o:p></P>
% a  s2 q3 [% h9 A( A<P >(7)算法7是错误的。<o:p></o:p></P>& O9 D# n* l6 g1 i
<P >在循环过程中,一旦出现<o:p></o:p></P>$ p8 z) v) z( g
<P >a
≤ x &lt; a[left + 1],则必进入死循环。<o:p></o:p></P>
/ i+ o. F8 |4 e/ ?' [<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 08:57 , Processed in 0.400087 second(s), 52 queries .

回顶部