Last active
April 9, 2020 08:12
-
-
Save olligobber/8c58c18c41da4771c160849b865c9163 to your computer and use it in GitHub Desktop.
Given a 0-terminated list of bytes, outputs them in increasing order. Uses 2n+4 bytes of memory to sort n bytes of input, using insertion sort.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WHILE INPUT IS NONZERO | |
,[ | |
MOVE TO LEFT ELEMENT | |
<< | |
WHILE LEFT ELEMENT EXISTS | |
[ | |
ORDER POS 0 AND POS 2 WITH SMALLEST ON RIGHT | |
USING POS 0 & 1 & 2 & 3 & 5 & 7 | |
WHILE POS 0 ISNT 0 | |
[ | |
MOVE TO POS 2 & COPY VALUE TO POS 3 & 5 | |
>>[->+>>+>>+<<<<<]>>>>>[-<<<<<+>>>>>]<<<<< | |
MOVE TO POS 3 | |
> | |
IF POS 3 ISNT 0 | |
[ | |
SUBTRACT ONE FROM POS 5 | |
>>-<< | |
SUBTRACT POS 3 FROM POS 5 | |
[->>-<<] | |
END IF | |
] | |
ADD ONE TO POS 5 | |
>>+<< | |
NOW POS 5 = ISZERO(POS 2) | |
IF ISZERO(POS 2) | |
>>[<< | |
MOVE TO POS 0 | |
<<< | |
MOVE POS 0 TO POS 2 | |
[->>+<<] | |
INCREMENT POS 0 & 2 & DECREMENT POS 1 | |
+>->+<< | |
RETURN TO POS 3 | |
>>> | |
RESET POS 5 TO 0 | |
>>-<< | |
END IF | |
>>]<< | |
MOVE TO POS 0 | |
<<< | |
SUBTRACT ONE FROM POS 0 & 2 | |
->>-<< | |
ADD ONE TO POS 1 | |
>+< | |
END WHILE | |
] | |
NOW POS 2 IS DIFF AND POS 1 IS MIN | |
MOVE POS 2 TO POS 0 | |
>>[-<<+>>]<< | |
ADD POS 1 TO POS 0 & 2 | |
>[-<+>>+<]< | |
THIS PAIR IS NOW ORDERED | |
MOVE TO LEFT ELEMENT OF NEXT PAIR | |
<< | |
END WHILE | |
] | |
LIST IS NOW SORTED | |
MOVE TO END OF LIST | |
>>[>>] | |
END WHILE | |
,] | |
OUTPUT ALL ELEMENTS | |
<<[.<<] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
,[<<[[>>[->+>>+>>+<<<<<]>>>>>[-<<<<<+>>>>>]<<<<[>>-<<[->>-<<]]>>+[<<<<<[->>+<<]+>->+>>>-]<<<<<->>-<+<]>>[-<<+>>]<[-<+>>+<]<<<]>>[>>],]<<[.<<] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment