📜 ⬆️ ⬇️

Sort Numbers by Brainfuck

In this article I will show you how to sort tsiferki using Brainfuck.

You enter numbers (each digit should not occur more than 255 times, with the 8bit version of the interpreter), after the program, if you can call it that, outputs them in ascending order.

This will be an implementation of sorting by counting , which works as follows:
1. Read the number k.
2. In array A, increment A [k] by one.
3. Repeat steps 1 and 2 until the numbers at the input end.
4. Print A [i] times the number i, where i is the possible values ​​of the numbers k.

')
Let's get started


We will read characters until we get a line break (ascii 10)

,----- ----- [ ----- ----- ----- ----- ----- ---     28 *     * ,----- ----- ] 


Now we need to move the pointer to the value we considered.

  [->+<]>       [ [->+<]    i-   i + 1 +>-  i- ,    ,   ] 


After that, the pointer will be on a cell equal to the initial value of the character that we entered. All cells, starting with 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: a shift of 10 cells is not accidental, think about what will happen without it.

Next, move to the zero cell.

  <[-<]  ,    


So, what we have done by implementing the first three steps of the algorithm.

 ,---------- [ ----- ----- ----- ----- ----- --- [->+<]> [ [->+<] +>- ] >>>>> >>>>> + <<<<< <<<<< <[-<] ,----- ----- ] 


Output answer


It remains the most important thing: to display sorted numbers.

But where are our numbers?

ASCII code '0' is 48.
After reading, we took 10, then another 28.
We moved the read value to the first cell.
But before we increased the cell, we moved 10 cells forward.
48 - 10 - 28 + 1 + 10 = 21
So the number of zeros will be in the 21st cells, the number of units in the 22nd, etc.

Let us assign the value of 48 to the twentieth cell (this is ascii zero code).
We display the value of the previous cell as many times as the value in the current cell.
Assign the current cell the value of the previous + 1.
We do this for every number.

 >>>>> >>>>> >>>>> >>>>>   20  ++++++++ ++++++++ ++++++++ ++++++++ ++++++++ ++++++++    48 >[<.>-] <[->+<]>+ ,     >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-]  ,     


Total


Here is the whole code
 ,---------- [ ----- ----- ----- ----- ----- --- [->+<]> [ [->+<] +>-] >>>>> >>>>> + <<<<< <<<<< <[-<] ,----- ----- ] >>>>> >>>>> >>>>> >>>>> ++++++++ ++++++++ ++++++++ ++++++++ ++++++++ ++++++++ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] <[->+<]>+ >[<.>-] 


In this simple way, we sorted the numbers.
It is easy to modify the code so that almost any characters are sorted, or to display numbers in descending order. Using this idea, you can sort not only numbers, but also 2 to 3 digit numbers, perhaps later I will write about it.

Thanks for attention.

Source: https://habr.com/ru/post/135262/


All Articles