QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2792|回复: 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>
8 q( [+ \5 j2 ^4 @1 ], z( j" O< ><FONT face="Times New Roman">public static int binarySearch1(int[] a, int x, int n)</FONT></P>
7 W: r, s8 g0 u: m# [+ z7 S< ><FONT face="Times New Roman">{</FONT></P>
8 L: ?* T/ a, G2 x< ><FONT face="Times New Roman">              int left = 0, right = n-1;</FONT></P>
/ t$ ~, Q- E2 F! S) e1 }< ><FONT face="Times New Roman">              while(left &lt;= right)</FONT></P>2 f% D2 X6 @2 X
< ><FONT face="Times New Roman">{</FONT></P>9 Z1 b3 I* g3 K* b0 C
< ><FONT face="Times New Roman">   int middle = (left + right) / 2;</FONT></P>
) w% W  N' [: I" }5 y( M% N) W< ><FONT face="Times New Roman">   if(x == a[middle]) return middle;</FONT></P>
* m: q/ G$ ]# W< ><FONT face="Times New Roman">   if(x &gt; a[middle]) left = middle;</FONT></P>
  d! {% j! `  |" i< ><FONT face="Times New Roman">   else right = middle;</FONT></P>" w9 q9 y2 @; H) S( x0 Y1 j* R" k
< ><FONT face="Times New Roman">}//while</FONT></P>
4 M+ c( u* i( S5 Y+ \< ><FONT face="Times New Roman">return –1;</FONT></P>
3 @) f* k1 U8 P6 b) v4 A5 ~< ><FONT face="Times New Roman">}</FONT></P>
2 `! x; b8 j9 Q$ @5 `< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
# O  e. K1 K2 w, |6 f0 j. {< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>* y0 @. {) s4 F% v
< ><FONT face="Times New Roman">public static int binarySearch2(int[] a, int x, int n)</FONT></P>8 l2 B3 T2 j) h' \1 k- L; V
< ><FONT face="Times New Roman">{</FONT></P>2 F. s" t" a( Y  q8 \* A
< ><FONT face="Times New Roman">              int left = 0, right = n-1;</FONT></P>
) o) }% m" D% N0 O< ><FONT face="Times New Roman">              while(left &lt; right-1)</FONT></P>; Y* ?5 m. |( g& O
< ><FONT face="Times New Roman">{</FONT></P>7 Y: W  K: W9 w9 s8 l
< ><FONT face="Times New Roman">   int middle = (left + right) / 2;</FONT></P>
7 H! k2 P# s3 Y" d7 |6 ~< ><FONT face="Times New Roman">   if(x &lt; a[middle]) right = middle;</FONT></P>
  |2 P! ~) e5 C2 q4 ^$ N: Z# p, W" A7 u< ><FONT face="Times New Roman">   else left = middle;</FONT></P>* O) ]* g: ]% k, I- H! J
< ><FONT face="Times New Roman">}//while</FONT></P>
( ~: ^$ q; B+ j" K6 ^" D5 t" }" X$ E< ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>) X0 G9 v+ x# O9 r" j9 P; `
< ><FONT face="Times New Roman">else return –1;</FONT></P>
" U9 `+ Y( l' \< ><FONT face="Times New Roman">}</FONT></P>
$ M5 [# C: o" i; W0 l! J< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
" N' h( m+ x1 Z# a0 I1 _1 p< ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>' d- q3 m' e8 P& L2 N8 E
< ><FONT face="Times New Roman">public static int binarySearch3(int[] a, int x, int n)</FONT></P>2 T, e+ C8 G+ E
<P ><FONT face="Times New Roman">{</FONT></P>
; h! G* D8 v% Z<P ><FONT face="Times New Roman">              int left = 0, right = n-1;</FONT></P>! t6 r: K1 A. K. d
<P ><FONT face="Times New Roman">              while(left+1 != right)</FONT></P>
+ M: W4 H; P. @  [: j2 R& I<P ><FONT face="Times New Roman">{</FONT></P>
/ H& w. ^5 s+ K, L# R<P ><FONT face="Times New Roman">   int middle = (left + right) / 2;</FONT></P>+ G; o0 W" B3 Y8 O( w& l8 ?
<P ><FONT face="Times New Roman">   if(x &gt;= a[middle]) left = middle;</FONT></P>4 q* J# z* h+ H3 `4 `
<P ><FONT face="Times New Roman">   else right = middle;</FONT></P>
! z# V* ]3 h; q) Z<P ><FONT face="Times New Roman">}//while</FONT></P>
4 j8 r1 {0 P- O<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
% M+ }! K0 n: W5 H- p; j) `! E<P ><FONT face="Times New Roman">else return –1;</FONT></P>
' w, ]3 S; u3 _3 q1 I! C4 A. u/ X<P ><FONT face="Times New Roman">}</FONT></P>
: L; S; m: @6 f' l. T1 H<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
, n* X1 f2 a- ^! R% @: u' i8 J<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>6 }% V/ O' x& Q2 i' ]
<P ><FONT face="Times New Roman">public static int binarySearch4(int[] a, int x, int n)</FONT></P>+ }1 t+ X4 e3 W  C; m0 `
<P ><FONT face="Times New Roman">{</FONT></P>; T+ @7 F& b) [+ w; i
<P ><FONT face="Times New Roman">              if(n &gt; 0 &amp;&amp; x &gt;= a[0])</FONT></P>
; c# l7 u! E3 l) ^  j<P ><FONT face="Times New Roman">              {</FONT></P>0 h; \1 b7 P. k2 n: D# {0 Z
<P ><FONT face="Times New Roman">                     int left = 0, right = n-1;</FONT></P>* n9 F; R. S: ^! E( H! N
<P ><FONT face="Times New Roman">              while(left &lt; right)</FONT></P>
+ A; C* |  v3 V8 E<P ><FONT face="Times New Roman">              {</FONT></P>
+ n9 ^. x! n4 u3 ^<P ><FONT face="Times New Roman">          int middle = (left + right) / 2;</FONT></P>
. E/ S& b+ l+ u$ _/ l% ~, S# J1 G2 K<P ><FONT face="Times New Roman">if(x &lt; a[middle]) right = middle - 1;</FONT></P>
; F( q# n3 K3 D5 o<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
* n$ h3 Z2 N5 d8 w+ W7 D9 A) i<P ><FONT face="Times New Roman">}//while</FONT></P>
) ]* N2 E* l% J+ W; k5 ~* D<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>9 t  q3 C( D6 C, Y' X
<P ><FONT face="Times New Roman">}//if</FONT></P>4 z5 p. T0 ]" A
<P ><FONT face="Times New Roman">return –1;</FONT></P>
& E5 z4 Z$ H% @* s3 n5 a# S<P ><FONT face="Times New Roman">}</FONT></P>
" q* o0 z' b% }5 |  A<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>1 s' y6 e  c" \) b9 o: P0 i
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>+ F: q5 H7 T9 V/ h+ a
<P ><FONT face="Times New Roman">public static int binarySearch5(int[] a, int x, int n)</FONT></P># F* x# i0 E2 i# r5 y: t
<P ><FONT face="Times New Roman">{</FONT></P>( {" t, x- |7 i, B5 Z6 @
<P ><FONT face="Times New Roman">              if(n &gt; 0 &amp;&amp; x &gt;= a[0])</FONT></P>: s0 m; J. f2 X6 Z  ]
<P ><FONT face="Times New Roman">              {</FONT></P>
8 Z! f( f: `, @0 l6 h<P ><FONT face="Times New Roman">                     int left = 0, right = n-1;</FONT></P>
6 p' B- d$ H  `) r$ ^) y<P ><FONT face="Times New Roman">              while(left &lt; right)</FONT></P>. Y3 [1 B8 L3 y: z7 ^4 X4 X
<P ><FONT face="Times New Roman">              {</FONT></P>/ L2 Y: T0 \+ j; p- [
<P ><FONT face="Times New Roman">          int middle = (left + right + 1) / 2;</FONT></P>
: V6 k4 [- l7 Q, {<P ><FONT face="Times New Roman">if(x &lt; a[middle]) right = middle - 1;</FONT></P>/ Q! T, B- I: A$ }4 }
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>
1 z) a* P- z7 `<P ><FONT face="Times New Roman">}//while</FONT></P>
& L' b* p( z! q2 x9 R) p<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
, I4 _8 v, e0 T/ r  e: j9 N<P ><FONT face="Times New Roman">}//if</FONT></P>- h- o( e7 M2 W/ g) Y! i
<P ><FONT face="Times New Roman">return –1;</FONT></P>! E3 Y& g: w# E( i+ n& W8 w
<P ><FONT face="Times New Roman">}</FONT></P>1 H$ t1 i9 K0 @9 p0 R
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>6 B% w4 o' J$ I0 D
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>7 i$ T$ e# w4 {: T& _" A7 Y6 `
<P ><FONT face="Times New Roman">public static int binarySearch6(int[] a, int x, int n)</FONT></P>
5 h  s, N- ^3 j# R) A& w' S<P ><FONT face="Times New Roman">{</FONT></P>
1 ^& l) m% m! g  g<P ><FONT face="Times New Roman">              if(n &gt; 0 &amp;&amp; x &gt;= a[0])</FONT></P>
! w7 q, p3 Y( |% ?5 |<P ><FONT face="Times New Roman">              {</FONT></P>
+ U! B. ~- m' n$ [! h<P ><FONT face="Times New Roman">                     int left = 0, right = n-1;</FONT></P>
( R# c0 F. ]# n) p8 h<P ><FONT face="Times New Roman">              while(left &lt; right)</FONT></P># @9 l0 Q9 T6 |! i& i+ L
<P ><FONT face="Times New Roman">              {</FONT></P>
3 N( t3 t4 G8 g( i+ O<P ><FONT face="Times New Roman">          int middle = (left + right + 1) / 2;</FONT></P>
" _* D2 z* U% R8 B<P ><FONT face="Times New Roman">if(x &lt; a[middle]) right = middle - 1;</FONT></P>
( U/ R3 _" \. X3 K1 i6 Y- k0 {<P ><FONT face="Times New Roman">else left = middle + 1;</FONT></P>
0 L! J" R8 c4 V% o3 [1 _<P ><FONT face="Times New Roman">}//while</FONT></P>
6 w4 i9 ]2 |) E0 q* {<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>
8 X" g& c& d( i; _" ]<P ><FONT face="Times New Roman">}//if</FONT></P>
9 x# ^2 u( d- P7 f4 |<P ><FONT face="Times New Roman">return –1;</FONT></P>
/ }+ Z1 s. a0 ]3 X<P ><FONT face="Times New Roman">}</FONT></P>
. n; p! s4 d) }0 Q' q* }<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
$ M: Y8 P% u! z; G+ S# e3 }6 N<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>1 n4 T" \* |6 W5 q. S0 [
<P ><FONT face="Times New Roman">public static int binarySearch7(int[] a, int x, int n)</FONT></P>
0 ^7 v9 C* F* r/ m. J9 R) D) F<P ><FONT face="Times New Roman">{</FONT></P>
" ?/ ~% R, w, D( }9 z<P ><FONT face="Times New Roman">              if(n &gt; 0 &amp;&amp; x &gt;= a[0])</FONT></P>' i' @/ C% w/ E7 H, p  e
<P ><FONT face="Times New Roman">              {</FONT></P>- d3 I' T9 d) _' s; C
<P ><FONT face="Times New Roman">                     int left = 0, right = n-1;</FONT></P>
! K- K6 _8 |/ Z( S<P ><FONT face="Times New Roman">              while(left &lt; right)</FONT></P>( \! D+ |+ x( g( t3 ?
<P ><FONT face="Times New Roman">              {</FONT></P>
1 O* f5 X! e. m* [- T<P ><FONT face="Times New Roman">          int middle = (left + right +1) / 2;</FONT></P>) v' K5 x, \8 ~( D. n$ T- l" J
<P ><FONT face="Times New Roman">if(x &lt; a[middle]) right = middle;</FONT></P>. Q$ O1 _& F2 \7 b0 h8 P
<P ><FONT face="Times New Roman">else left = middle;</FONT></P>" _1 m( @3 m1 A0 w' L. u
<P ><FONT face="Times New Roman">}//while</FONT></P>
6 k' d( z2 |! u' r, v0 E<P ><FONT face="Times New Roman">if(x == a
) return left;</FONT></P>4 A, n( _. p4 |8 g9 s0 @
<P ><FONT face="Times New Roman">}//if</FONT></P>2 m; J/ X/ w4 y: p
<P ><FONT face="Times New Roman">return –1;</FONT></P>
3 x3 a/ F3 T+ k<P ><FONT face="Times New Roman">}</FONT></P># l5 q8 a" k& U, N. ^
<P ><FONT face="Times New Roman"> <o:p></o:p></FONT></P>
9 U' h. x( X- ~* p<P >解:(<FONT face="Times New Roman">1</FONT>)算法<FONT face="Times New Roman">1</FONT>不正确。<o:p></o:p></P>6 p8 _' Q0 S) V" X2 P
<P >当在数组<FONT face="Times New Roman">a</FONT>中找不到与<FONT face="Times New Roman">x</FONT>相等的元素时,算法将进入死循环状态。<o:p></o:p></P>- N& e, E' a9 Z" X0 T( I: R
<P >原因:每次循环时,变量<FONT face="Times New Roman">left</FONT>和<FONT face="Times New Roman">right</FONT>的值修改不正确。应修改如下:<o:p></o:p></P>7 z. x  a6 m, U, h0 h; A
<P ><FONT face="Times New Roman">if(x &gt; a[middle]) left = middle + 1;<o:p></o:p></FONT></P>: T, e  D; q! W1 [5 c
<P ><FONT face="Times New Roman">       else right = middle - 1;<o:p></o:p></FONT></P>
4 X  d+ w1 g# s! a$ C  q+ T<P >(<FONT face="Times New Roman">2</FONT>)算法<FONT face="Times New Roman">2</FONT>不正确。<o:p></o:p></P>3 n5 c+ o+ ^0 s5 g2 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>
, a. D2 c, ?( Q8 N<P >另外,当<FONT face="Times New Roman">n=0</FONT>时执行<FONT face="Times New Roman">if(x == a
)...</FONT>时将出现下标越界错误。<o:p></o:p></P>
8 K! [5 M; h( \' i4 x) Y1 o<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>( H' Q7 j2 ?" E$ A1 s- a) p
<P >(<FONT face="Times New Roman">3</FONT>)算法<FONT face="Times New Roman">3</FONT>不正确。<o:p></o:p></P>- G! V$ C+ W- R
<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>
# v' `8 E! ?9 T1 W9 I5 g, ?<P >原因:与算法<FONT face="Times New Roman">2</FONT>相同。<o:p></o:p></P>
" |" j; D1 F7 x) q& i5 f<P >(<FONT face="Times New Roman">4</FONT>)算法<FONT face="Times New Roman">4</FONT>不正确。<o:p></o:p></P>: Z* E6 V6 p: I0 |! S5 E/ ]
<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>7 N! C0 d& {- U4 t0 v9 e
<P >原因:循环条件和对变量<FONT face="Times New Roman">left</FONT>值的修改有错误。<o:p></o:p></P>
" K9 d' O, b( D- A' S. K) c5 X* }<P >(<FONT face="Times New Roman">5</FONT>)此算法正确。<o:p></o:p></P>0 y" ?: Y( g+ H9 f
<P >证明:当<FONT face="Times New Roman">n=0</FONT>或<FONT face="Times New Roman">n=1</FONT>时,算法显然正确。<o:p></o:p></P>8 W8 m; \, z. |9 q5 Z
<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>3 j3 c6 d4 V2 n/ S! m; r3 \- D
<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>
/ p4 S, r" p4 y" e<P >即:middle &gt; left成立。<o:p></o:p></P>
+ w9 v& U% P: H) @8 k8 C<P >且<FONT face="Times New Roman">middle = (left + right + 1) / 2 = [(left + 1) + right] / 2 </FONT>≤ 2right / 2 = right,<o:p></o:p></P>( A! |3 I" ^4 G) d. x; F% e: u. h9 u" }
<P >∴left &lt; middle ≤ right恒成立。<o:p></o:p></P>; m# V% @1 u$ K3 l
<P >因此,每次循环之后,right与left之差必然减小,在有限次循环后,必有left = right条件成立,从而循环结束。<o:p></o:p></P>0 ?/ O3 J2 y5 i' z. U
<P >如果x值与数组a的某个元素值相等,则在循环结束时显然有x = a
且x = a
成立,否则x ≠a
,即未找到x,<o:p></o:p></P>3 ~2 v2 c8 R" Q' X4 j# Q4 q
<P >∴返回结果正确。<o:p></o:p></P>. e7 o2 I/ l7 \5 r
<P >(6)算法6是错误的。<o:p></o:p></P>
9 r9 n' M# G( A6 s1 i4 o<P >当执行到某次循环x = a[middle]成立时,再执行if 语句中的<o:p></o:p></P>
+ H: j6 F+ W* j3 Q2 O6 b<P >left = middle + 1;<o:p></o:p></P>/ k/ v8 Z* s3 E: R; m5 x: D
<P >就把结果丢失了,导致错误。而且还可能会导致下标越界错误。例如:<o:p></o:p></P>1 g7 \% N% V! i1 p: [; ~
<P >当n = 2且x = a[1]时即会出现这些情况。<o:p></o:p></P>
4 f! s4 T% X' q2 r# X" D( E3 n<P >原因:if 语句中的left = middle + 1;应改为left = middle;<o:p></o:p></P>
1 L: U3 A9 ?; x. ^, G( V) H<P >(7)算法7是错误的。<o:p></o:p></P>: e. B0 `9 s* P4 ^' s, o' r- }
<P >在循环过程中,一旦出现<o:p></o:p></P>
! _! h  l/ g  J7 g7 m: _* x<P >a
≤ x &lt; a[left + 1],则必进入死循环。<o:p></o:p></P>) S4 j" [1 e$ U! e& p( p$ I
<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-4-20 07:15 , Processed in 0.441731 second(s), 53 queries .

回顶部