-- HUMAN RESOURCE MACHINE PROGRAM --
-- 41-Sorting-Floor - SIZE 69/34 - SPEED 1534/714 --

-- This is merge sort. In HRM it is extremely impractical, but cool nonetheless.
-- It uses a non-recursive approach, first sorting groups of size 2, then 4, 8 
-- and so on. Because the merging is done naively and in-place, the algorithm
-- actually runs in O(n^2 log n), which is worse than the simple sorts. Memory
-- constraints make it difficult to have an auxiliary array to do merging in.
-- VARIABLES --
-- n is the number of elements, size is the current group size, but divided by
-- two - merging into groups of two means size = 1.
-- i and j represent the indices in the first and second group, respectively.
-- to is the last element of the group that is being merged.
-- The variables t, k and cur are helper variables used in merging.

a:
    COPYFROM 24
    COPYTO   21
b:
    INBOX   
    JUMPZ    c
    COPYTO   [21]
    BUMPUP   21
    JUMP     b
c:
    COPYFROM 24
    COPYTO   20
    BUMPUP   20
    COMMENT  4
d:
    COPYFROM 24
    COPYTO   16
    COPYFROM 21
    SUB      20
    JUMPN    n
    COMMENT  0
e:
f:
g:
    COPYFROM 16
    SUB      21
    COMMENT  7
    JUMPZ    m
    COPYFROM 16
    COPYTO   22
    ADD      20
    COPYTO   23
    ADD      20
    COPYTO   16
    SUB      21
    JUMPN    h
    COMMENT  3
    COPYFROM 21
    COPYTO   16
h:
i:
    COMMENT  2
    COPYFROM 22
    SUB      23
    JUMPZ    e
    COPYFROM 16
    SUB      23
    COMMENT  5
    JUMPZ    f
    JUMPN    g
    COPYFROM [22]
    SUB      [23]
    JUMPN    l
    COMMENT  1
    COPYFROM [23]
    COPYTO   17
    COPYFROM 23
    COPYTO   18
j:
    BUMPDN   18
    COPYFROM [18]
    COPYTO   19
    BUMPUP   18
    COPYFROM 19
    COPYTO   [18]
    BUMPDN   18
    SUB      22
    JUMPZ    k
    JUMP     j
k:
    COPYFROM 17
    COPYTO   [22]
    BUMPUP   23
l:
    BUMPUP   22
    JUMP     i
m:
    COPYFROM 20
    ADD      20
    COPYTO   20
    JUMP     d
    COMMENT  6
n:
    COPYFROM 24
    COPYTO   22
o:
    COPY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