For those interested (if not for a RC contribution then for the problem itself).
The merge sort task is still open (I wonder why: ok it's ridiculous to use
something else as /:~ y). For a start, here's my translation of the Haskell
contribution.
merge =: ({...@] , ($: }.)) `({...@[ , ($: }.)~) @. (<:&{.) ` , @. (+.&(0=#))
split =: </.~ 0 1$~#
mergeSort =: merge & $: &>/@ split ` ] @. (1>:#)
But I'm not very happy with the use of the merge verb (too soon into stack
problems).
mergeSort 1 5 2 8 3 4 9 6 7
1 2 3 4 5 6 7 8 9
a=. 33e2 ?...@#1000
mergeSort a
0 0 0 0 0 0 1 1 1 1 1 2 3 3 3 3 3 3 4 4 4 4 4 5 5 5 5 6 6 .....
a=. 34e2 ?...@#1000
mergeSort a
|stack error: merge
| mergeSort a
If you want to know what's going on, use the following explicit verb instead of
the tacit 'merge'
merge=: 4 : 0
smoutput x;y
if. (0=#x) +. 0=#y do.
smoutput x,y
x,y
return.
end.
if. x <:&{. y do.
smoutput {.x
x ({...@[, (merge }.)~) y
else.
smoutput {.y
x ({...@], (merge }.)) y
end.
)
Don't use too many elements.
mergeSort 1 5 7 3 8
+-+-+
|5|3|
+-+-+
3
+-++
|5||
+-++
5
...
...
5
+---++
|7 8||
+---++
7 8
1 3 5 7 8
Any ideas? For now speed is not an issue, but at the end ... ok. ;-)
--
Met vriendelijke groet,
=@@i
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm