Hi Ladislav, 15-Jul-2000 you wrote:

>Hi,

>don't know, what is the problem, but here is a merge-sort working
>for me looong time:
[...]

Thanks! In the meantime, I got it working by simply having the temporary
variables as parameters to the function. Mean, but it works.

As a curiosity, I also got the version of merge-sort working which uses less
memory. I'll attach it here, just in case anybody finds it interesting. It's
less artistic, but I guess that's just the price you have to pay for using
less memory.

Kind regards,
--
Ole Friis <[EMAIL PROTECTED]>

Amiga is a trademark of Amiga Inc.
; Merge sorting, uses only memory proportional to the length of list,
; instead of memory proportional to (length of list)*log(length of list)
sort-list: function [list cfunc] [sort-list2 low2] [
  sort-list2: func [list list2 cfunc low high middle] [
    if (high - low) > 1 [
      ; Sort recursively
      middle: to-integer (low + high) / 2
      sort-list2 list2 list :cfunc low middle 0
      sort-list2 list2 list :cfunc middle high 0

      ; Merge list2[low..middle) and list2[middle..high) into list[low..high]
      list: skip list (low - 1)
      low2: middle
      while [(low < middle) and (low2 < high)] [
        either cfunc (pick list2 low) (pick list2 low2) [
          change list pick list2 low
          low: low + 1
        ] [
          change list pick list2 low2
          low2: low2 + 1
        ]
        list: next list
      ]
      ; Append the missing items
      for low low (middle - 1) 1 [
        change list pick list2 low
        list: next list
      ]
      for low2 low2 (high - 1) 1 [
        change list pick list2 low2
        list: next list
      ]
    ]
  ]

  sort-list2 list (copy list) :cfunc 1 (length? list) + 1 0
]

Reply via email to