【程序员升职记系列】排序楼层

第 41 关,排序楼层。指令集介绍请看:https://www.annhe.net/article-3828.html

优化目标

34 行 714 步。

代码

28行537步

插入排序,见 Github

20行648步

选择排序,先全部读到地毯上,然后找出最小的输出,字符串最后一个填充输出的最小数的位置,字符串长度减1,直至长度为0。见 Github

28行658步

-- 大数放在前面,从后往前输出
-- 20 存放读入的数

JUMP     init

out1:
BUMPUP   24

init:
BUMPDN   24

next:
BUMPUP   24
COPYTO   22
COPYTO   21
INBOX
JUMPZ    out
COPYTO   20
COPYTO   [22]
BUMPDN   21
JUMPN    next

insert:
COPYFROM 20
SUB      [21]
JUMPN    next
COPYFROM [21]
COPYTO   [22]
COPYFROM 20
COPYTO   [21]
BUMPDN   22
BUMPDN   21
JUMPN    next
JUMP     insert


out:
BUMPDN   24
JUMPN    out1
COPYFROM [24]
OUTBOX
JUMP     out

28行663步

-- 大数放在前面,从后往前输出
-- 20 存放读入的数

init:
COPYFROM 24
-- 字符串下标
COPYTO   23
-- 待比较下标
BUMPDN   23

next:
BUMPUP   23
COPYTO   22
COPYTO   21
INBOX
JUMPZ    out
COPYTO   20
COPYTO   [22]
BUMPDN   21
JUMPN    next

insert:
COPYFROM 20
SUB      [21]
JUMPN    next
COPYFROM [21]
COPYTO   [22]
COPYFROM 20
COPYTO   [21]
BUMPDN   22
BUMPDN   21
JUMPN    next
JUMP     insert


out:
BUMPDN   23
JUMPN    init
COPYFROM [23]
OUTBOX
JUMP     out

第一次pass 40行1225步

init:
COPYFROM 24
COPYTO   20
COPYTO   19
COPYTO   18

start:
INBOX
COPYTO   [20]
JUMPZ    sortpre
BUMPUP   20
JUMP     start

sortpre:
BUMPDN   20
COPYTO   22
JUMPZ    out

sort:
COPYFROM [20]
COPYTO   23
COPYFROM 20
COPYTO   21
-- 19号存储已经排序的数量,20号如果和19号相等,就不需要在往前比较了,从结尾重新开始一轮冒泡
SUB      19
JUMPZ    next
BUMPDN   20
JUMPN    next
COPYFROM 23
SUB      [20]
JUMPN    bubble
JUMP     sort

bubble:
COPYFROM [20]
COPYTO   [21]
COPYFROM 23
COPYTO   [20]
JUMP     sort

next:
BUMPUP   19
SUB      22
JUMPZ    out
COPYFROM 22
COPYTO   20
JUMP     sort

out:
COPYFROM [18]
JUMPZ    init
OUTBOX
BUMPUP   18
JUMP     out

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注