在Pharo合并排序

时间:2017-10-06 14:02:46

标签: pharo

抱歉我的英语不好。 所以,我的合并排序有问题。 这是合并部分:

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) ]
      ]

这是我的递归函数:

| 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.
                               ]
     ]

我在排序部分遇到了问题。请帮帮我。

1 个答案:

答案 0 :(得分:0)

似乎你误解了#ifTrue:ifFalse:和那个系列的消息。

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

在您的示例中,您有

i to: deb1 do: [
    condition 
     ifTrue: [  ]
     ifFalse: [ condition2 ifTrue: [ actions ] ];
     ifFalse: [ condition3 ifTrue: [ other actions ];
     ifFalse: [ an action more ] 

但是发送到相同的布尔条件的第一个ifTrue:ifFalse :(顺便说一下,它只是放入ifFalse:因为ifTrue:part中的空块)。然后,ifFalse:再次,两次,到同一个对象(因为那就是;意味着)。条件没有被重新评估,所以如果它第一次是真的,什么也没做,如果它是假的,那么将执行三个动作。 它相当于只有一个ifFalse,连接块内的所有动作。 ......我不认为这就是你的意图。

此外,你还有那个

i := deb1.
i to: deb1 do: [ ... ]

并使用相同的值,将deb1作为:deb1。 (这甚至会运行吗?我认为该块需要参数)

相关问题