QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2898|回复: 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>
4 }. S$ v# I3 ^0 I( H1 h9 v< ><FONT face="Times New Roman">public static int binarySearch1(int[] a, int x, int n)</FONT></P>
0 V" [8 n% Z5 b) o+ W% h8 {+ `< ><FONT face="Times New Roman">{</FONT></P>: j! e4 R+ l# J7 N4 ~
< ><FONT face="Times New Roman">              int left = 0, right = n-1;</FONT></P>% ^1 ]% r6 Y" ?2 H
< ><FONT face="Times New Roman">              while(left &lt;= right)</FONT></P>! ~* D* M* p/ \6 P* |0 \
< ><FONT face="Times New Roman">{</FONT></P>/ v6 C$ l: c" s
< ><FONT face="Times New Roman">   int middle = (left + right) / 2;</FONT></P>& \2 z( D5 I- Z! P" g
< ><FONT face="Times New Roman">   if(x == a[middle]) return middle;</FONT></P>
$ M2 ^# ^; m; S1 D% ]  y/ H8 i< ><FONT face="Times New Roman">   if(x &gt; a[middle]) left = middle;</FONT></P>0 [# \3 J9 c; Z
< ><FONT face="Times New Roman">   else right = middle;</FONT></P>
0 Y) C" z; c  A9 B< ><FONT face="Times New Roman">}//while</FONT></P>1 D! E! @5 S$ t, B0 b3 j
< ><FONT face="Times New Roman">return –1;</FONT></P>2 k) O, m3 S4 @, N5 \
< ><FONT face="Times New Roman">}</FONT></P>
' H+ \0 y0 B  y) K) r< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
+ ^* d$ q1 d* Y2 P< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
0 q" `* _+ Z! A< ><FONT face="Times New Roman">public static int binarySearch2(int[] a, int x, int n)</FONT></P>
& \$ I9 Y( W  h/ N$ o( V8 D. d< ><FONT face="Times New Roman">{</FONT></P>
& ]4 B# n5 |/ p9 m4 {< ><FONT face="Times New Roman">              int left = 0, right = n-1;</FONT></P>
7 i! x- _8 Q! A: c5 V< ><FONT face="Times New Roman">              while(left &lt; right-1)</FONT></P>
9 @! D$ V( F! C1 m5 V$ N< ><FONT face="Times New Roman">{</FONT></P>
' c5 g" k  s# o, t' m% ?8 A' |: Y% d< ><FONT face="Times New Roman">   int middle = (left + right) / 2;</FONT></P>
/ ^& `& q# d( r9 m< ><FONT face="Times New Roman">   if(x &lt; a[middle]) right = middle;</FONT></P>) K& q4 y6 {' x8 k; l& h* X7 @% g. s
< ><FONT face="Times New Roman">   else left = middle;</FONT></P>
5 o6 f" T/ n6 R3 L5 m$ G+ R< ><FONT face="Times New Roman">}//while</FONT></P>
' I0 I# s. K$ Y# `8 x2 m) T; ?< ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
8 w* Y  W* W1 k! D) m; m0 e< ><FONT face="Times New Roman">else return –1;</FONT></P>
5 K3 I7 }$ A4 ]6 a7 A0 n< ><FONT face="Times New Roman">}</FONT></P>
( z. l( s6 j' I: v< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
8 U) P5 Q" ^7 x9 n/ M' }9 m< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
2 e/ y4 W; u7 G< ><FONT face="Times New Roman">public static int binarySearch3(int[] a, int x, int n)</FONT></P>+ ]! b9 a  s6 h& }" j( }/ M3 Z
<P ><FONT face="Times New Roman">{</FONT></P>
8 s& W4 m, M+ E<P ><FONT face="Times New Roman">              int left = 0, right = n-1;</FONT></P>/ S* L. j* j# d$ B& k
<P ><FONT face="Times New Roman">              while(left+1 != right)</FONT></P>, H" }  C7 j1 i6 @/ r- D: S" F4 V1 ~
<P ><FONT face="Times New Roman">{</FONT></P>; @& }, R, z0 ]/ ^
<P ><FONT face="Times New Roman">   int middle = (left + right) / 2;</FONT></P>+ K2 d' k( n) a
<P ><FONT face="Times New Roman">   if(x &gt;= a[middle]) left = middle;</FONT></P>
) o5 E4 R1 U% b# }( w3 w* ^/ B8 C<P ><FONT face="Times New Roman">   else right = middle;</FONT></P>
3 Z0 q7 e2 e2 L- D' R1 o<P ><FONT face="Times New Roman">}//while</FONT></P>
7 D9 M1 `& F1 L0 m# F: s<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
/ m( U2 k2 g$ f) F<P ><FONT face="Times New Roman">else return –1;</FONT></P>2 ~) ?" u0 q/ U. a: R
<P ><FONT face="Times New Roman">}</FONT></P>
( y$ D: z" r# f<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>: b' L1 J' I3 n; l
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
, b: f# e7 i" `7 b% l2 P8 ]" C/ ?<P ><FONT face="Times New Roman">public static int binarySearch4(int[] a, int x, int n)</FONT></P>! z- q9 Y4 w3 S5 u" z  M
<P ><FONT face="Times New Roman">{</FONT></P>: B2 A( c; q' M+ j
<P ><FONT face="Times New Roman">              if(n &gt; 0 &amp;&amp; x &gt;= a[0])</FONT></P>
2 E8 _9 D8 ]6 h8 ]<P ><FONT face="Times New Roman">              {</FONT></P>1 a6 _. O4 r- T$ W$ Y
<P ><FONT face="Times New Roman">                     int left = 0, right = n-1;</FONT></P>& x  d6 \8 R% p6 o
<P ><FONT face="Times New Roman">              while(left &lt; right)</FONT></P>
& v4 f+ W6 t, k0 q: P7 f<P ><FONT face="Times New Roman">              {</FONT></P>5 ]1 w/ Q& f% `  r' [2 j/ a
<P ><FONT face="Times New Roman">          int middle = (left + right) / 2;</FONT></P>7 [$ N: @" W1 y* T' @( K: O
<P ><FONT face="Times New Roman">if(x &lt; a[middle]) right = middle - 1;</FONT></P>
5 v( m) J/ i9 \- _) o8 ]<P ><FONT face="Times New Roman">else left = middle;</FONT></P>. b7 x6 K- |5 U2 o$ B, _
<P ><FONT face="Times New Roman">}//while</FONT></P>
6 H/ u, p- u+ j! }: ]<P ><FONT face="Times New Roman">if(x == a
) 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 &gt; 0 &amp;&amp; x &gt;= 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 &lt; 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 &lt; 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 &gt; 0 &amp;&amp; x &gt;= 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 &lt; 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 &lt; 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 &gt; 0 &amp;&amp; x &gt;= 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 &lt; 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 &lt; 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 &gt; 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 &lt;= 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 &lt; 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 &gt; 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 &lt; 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 &lt; a[left + 1],则必进入死循环。<o:p></o:p></P>
# a: _% B3 F( w# R( [2 q" a' |<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-10-24 04:13 , Processed in 2.149983 second(s), 53 queries .

回顶部