Generate binary reflection code

Posted by shazly on Sat, 19 Feb 2022 12:00:12 +0100

Recursively generate binary reflection Code:

Requirements:

We use recursion to generate binary reflection gray code.

What is gray code?

The typical Binary Gray Code is abbreviated as gray code, which is named after the patent "Pulse Code Communication" of Frank Gray (18870913-19690523) published in 1953. It was originally used to signal communication , now commonly used for simulation - Digital conversion And position - digital conversion in progress. The baud code used by French telecommunication engineer Jean Maurice É mile Baudot (18450911-19030328) in 1880 is equivalent to a deformation of it. An 8-yuan binary mechanical counter designed by George Stibitz in 1941 just conforms to Gray Counter Counting rule of.

Gray code has used the names of gray code, gray code, gray code, gray code, gray code, cyclic code, binary reflection code, minimum error code and so on. Some of them are wrong and some are easy to be confused with other names. It is recommended not to use them.

Stick from Baidu Encyclopedia (manual dog head)

In short, gray code is applied in various occasions. What we need to do is to generate it. Its feature is that only one of the two adjacent numbers is different. This requires us to think a little bit.

Based on the following pseudo code:

• I'm stupid to see this pseudo code, because it's not good-looking at all. The method doesn't even give a return value, and I have to design it myself. It's so fucked.

Because I use Java, the return value designed for this problem is list < string >. The specific codes are as follows

```/**
* @program:Algorithm library
* @description:Generation class of binary reflection gray code
*/
public class BinaryReflectedGrayCode {

public List<String> BRGC(int n){
List<String> L1;
if(n==1){
L1 = new ArrayList<String>();
return L1;
}else{
L1 = new ArrayList<String>(BRGC(n-1));
List<String> L2 = new ArrayList<>();
int len = L1.size();
for (int i = 0; i < len; i++) {
}
for(int i=0;i<len;i++){
String s = L1.get(i);
s = "0"+s;
L1.remove(i);
}
int len2 = L2.size();

for (int i = 0; i < len2; i++) {
}
return L1;
}
}
}
```

The specific steps are:

• Because it is a recursive algorithm, we must first specify the end point of recursion, that is, our initial condition. When n==1, we return two results: 0 and 1
• Then you can operate step by step according to the prompts given by the pseudo code.

Here, when reversing the List form, you can use collections Reverse (parameter), but the return value of this method is void, so it is finally returned to our parameters.

In the subsequent steps, the operation we passed in is still useful, so we can't replace it. In that case, the operation cost is too high.

```public static void reverse(List<?> list) {
int size = list.size();
if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) {
for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--)
swap(list, i, j);
} else {
// instead of using a raw type here, it's possible to capture
// the wildcard but it will require a call to a supplementary
// private method
ListIterator fwd = list.listIterator();
ListIterator rev = list.listIterator(size);
for (int i=0, mid=list.size()>>1; i<mid; i++) {
Object tmp = fwd.next();
fwd.set(rev.previous());
rev.set(tmp);
}
}
}
```
• The implementation of this method in the Java source code is attached here, and the underlying layer is exchanged, so we can't use it like this.

Therefore, the exchange here is to use the loop. After directly traversing the loop, save it in our L2 set, and then update the elements in L1. The update here should be that there should be no library function to use, so we write it according to the regular operation of update, remove it first and then assign a value. After this operation, we finish what we need to do

Finally, we can merge L1 and L2. Here we traverse L2 and add L2 to L1.

Maybe, maybe, maybe it's a relatively simple scheme.

Topics: Java Algorithm data structure