Sort numbers on Brainfuck
You enter numbers (each digit should not occur more than 255 times, with an 8bit version of the interpreter), after the program, if you can call it that, displays them in ascending order.
This will be the implementation of counting sorting , which works as follows:
1. The number k is read.
2. In array A, increase A [k] by one.
3. Repeat steps 1 and 2 until the input numbers run out.
4. We print A [i] times the number i, where i are the possible values of the numbers k.
Let's get started
We will read the characters until we get the line feed (ascii 10)
,----- -----
[
----- ----- ----- ----- ----- --- уменьшаем значение ячейки на 28
*тут будет работа со введенными данными*
,----- -----
]
Now we need to move the pointer to the value that we considered.
[->+<]> перемещаем значение нулевой ячейки в первую
[
[->+<] перемещаем значение из i-ой ячейки в i + 1
+>- увеличиваем i-ую ячейку, переходим в следующую ячейку, уменьшаем её
]
After that, the pointer will be on the cell equal to the original value of the character that we entered. All cells, starting from the first, ending with the one before which the pointer will be filled with units - this will help us return to the zero cell.
Well, now it's time to increase the value of the cell, according to the second step of the algorithm.
>>>>> >>>>> + <<<<< <<<<<
Note: the shift of 10 cells is not accidental, think what would happen without it.
Next, move to the zero cell.
<[-<] спасибо единичкам, которые мы оставляли
So, what we got by implementing the first three steps of the algorithm.
,----------
[
----- ----- ----- ----- ----- ---
[->+<]>
[
[->+<]
+>-
]
>>>>> >>>>> + <<<<< <<<<<
<[-<]
,----- -----
]
Response output
The most important thing remains: to display the sorted numbers.
But where are our numbers?
ASCII code '0' is 48.
After reading, we subtracted 10, then another 28.
We moved the read value to the first cell.
But before the cell was enlarged, we moved forward 10 cells.
48 - 10 - 28 + 1 + 10 = 21
So the number of zeros will lie in the 21st cell, the number of units in the 22nd, etc.
Set the twentieth cell to 48 (this is the ascii code of zero).
We display the value of the previous cell as many times as the value in the current cell.
Assign the current cell to the value of the previous + 1.
Do this for each digit.
>>>>> >>>>> >>>>> >>>>> переходим на 20ую ячейку
++++++++ ++++++++ ++++++++ ++++++++ ++++++++ ++++++++ увеличиваем ячейку до 48
>[<.>-] <[->+<]>+ выводим, переходим к следующей ячейке
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] выводим девятки, и наконец программа завершается
Total
Here is the whole code
,----------
[
----- ----- ----- ----- ----- ---
[->+<]>
[ [->+<] +>-]
>>>>> >>>>> + <<<<< <<<<<
<[-<]
,----- -----
]
>>>>> >>>>> >>>>> >>>>>
++++++++ ++++++++ ++++++++ ++++++++ ++++++++ ++++++++
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-] <[->+<]>+
>[<.>-]
In such a simple way, we sorted the numbers.
It is easy to modify the code so that almost any characters are sorted, or display numbers in descending order. Using this idea, you can sort not only numbers, but also 2 - 3-digit numbers, maybe later I will write about it.
Thanks for attention.