首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Pharo中合并排序

在Pharo中合并排序
EN

Stack Overflow用户
提问于 2017-10-06 22:02:46
回答 1查看 72关注 0票数 1

对不起,我的英语很差。所以,我的合并排序有一个问题。这是合并部分:

代码语言:javascript
复制
fusion: nTableau debut: deb1 fin1: fin1 fin2: fin2

| deb2 compt1 compt2 i t |

t := #().
deb2 := fin1 + 1.
compt1 := deb1.
compt2 := deb2.
i:= deb1.

(i to: fin1) do: [ t at:(i-deb1) put: (nTableau at:i) ].

i := deb1.
i to: deb1 do: [
    (compt1 = deb2) ifTrue: [  ]
    ifFalse:  [ compt2 =(fin2 +1)ifTrue: [ nTableau at:i put: (t at: compt1-deb1). compt1 := compt1+1 ] ];
    ifFalse: [ (t at: (compt1-deb1) < nTableau at: compt2) ifTrue: [ nTableau at:i put: (t at:(compt1 - deb1)). compt1 := compt1 +1 ]  ];
    ifFalse: [ nTableau at:i put: (nTableau at:compt2) ]
      ]

这是我做递归的排序部分:

三重

代码语言:javascript
复制
| trier milieu |
trier := nil.
trier := [ :tab :deb :fin |
                [ deb := (taille/taille). fin = taille. tab := tableau ].
                [ milieu := ( deb + fin ) //                    
                                    (deb ~= fin) ifTrue: [ 
                                        trier value: tab value: deb value: milieu.
                                        trier value: tab value: milieu+1 value: fin. 
                                        fusion: tab deb: debut fin1: milieu fin2: fin.
                               ]
     ]

我在排序部分有问题。请帮帮我。

EN

回答 1

Stack Overflow用户

发布于 2017-10-07 01:40:23

看起来你误解了#ifTrue:ifFalse:和那个消息家族做的事情。

它们不是句法结构,而是发送给对象并受其影响的消息;作为级联。

在您的示例中,您有

代码语言:javascript
复制
i to: deb1 do: [
    condition 
     ifTrue: [  ]
     ifFalse: [ condition2 ifTrue: [ actions ] ];
     ifFalse: [ condition3 ifTrue: [ other actions ];
     ifFalse: [ an action more ] 

但这会将第一个ifTrue:ifFalse:发送给相同的布尔条件(顺便说一句,这与只将ifFalse:放入ifTrue:部分中的空块相同)。然后,ifFalse:再重复两次,指向同一个对象(因为这就是;的意思)。该条件不会重新计算,因此如果第一次为真,则不执行任何操作,如果为假,则将执行三个bocks操作。它相当于只有一个ifFalse,将块内的所有操作连接起来。...and我不认为这是你的本意。

另外,在那之前你也有过

代码语言:javascript
复制
i := deb1.
i to: deb1 do: [ ... ]

在这里,您使用相同的值,执行deb1 to: deb1。(它甚至可以运行吗?我认为代码块需要一个参数)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46607568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档