Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using recursion to print permutations of a bit string of length N in Java

Tags:

java

recursion

I'm trying to obtain a linked list of integer arrays representing the possible permutations of a bit string of length N e.g for N = 2

00 01 10 11

I successfully wrote the code representing the bits as strings like so:

public static LinkedList<String> printBin(String soFar, int iterations) {
    if(iterations == 0) {
        LinkedList<String> ret = new LinkedList<String>();
        ret.add(soFar);
        return ret;
    }else {
        LinkedList<String> ret = new LinkedList<String>();
        ret.addAll(printBin(soFar + "0", iterations - 1));
        ret.addAll(printBin(soFar + "1", iterations - 1));
        return ret;
    }
}

However i tried to convert this code to work with arrays of integers and as a result for N = 2 I get

11 11 11 11

SSCE below:

public class Main {
    public static void main(String[] args){
        int numberOfBits = 2;
        LinkedList<int []> results = printBin(new int[numberOfBits], 0, numberOfBits);
        Iterator<int[]> i = results.iterator();
        while(i.hasNext()){
            int[] temp = i.next();
            for(int j = 0; j < temp.length; j++){
                System.out.print(temp[j]);
            }
            System.out.println("");
        }
    }


    public static LinkedList<int[]> printBin(int[] soFar, int counter, int iterations) {
        if(iterations == 0) {
            LinkedList<int[]> ret = new LinkedList<int[]>();
            ret.add(soFar);
            return ret;
        }else {
            LinkedList<int[]> ret = new LinkedList<int[]>();
            int[] soFar1 = soFar; soFar1[counter] = 0;
            int[] soFar2 = soFar; soFar2[counter] = 1;
            ret.addAll(printBin(soFar1, counter + 1, iterations - 1));
            ret.addAll(printBin(soFar2, counter + 1, iterations - 1));
            return ret;
    }
}

}

Any help would be greatly appreciated.

like image 881
John Walker Avatar asked Jan 27 '26 04:01

John Walker


1 Answers

The problem with your code is that it keeps reusing the same instance of the array soFar on each level of invocation, rather than making a copy. If you change the code to

int[] soFar1 = (int[])soFar.clone(); soFar1[counter] = 0;
int[] soFar2 = (int[])soFar.clone(); soFar2[counter] = 1;

your code should produce the results that you want (demo on ideone).

However, you would be better off iterating all integers from 0 to (1 << numberOfBits) - 1, and printing their binary representations:

LinkedList<int[]> ret = new LinkedList<int[]>();
int endMask = 1 << numberOfBits;
for (int mask = 0 ; mask != endMask ; mask++) {
    int[] combo = new int[numberOfBits];
    for (int i = 0 ; i != numberOfBits ; i++) {
        combo[i] = ((mask & (1 << i)) != 0) ? 1 : 0;
    }
    ret.add(combo);
}

Here is a demo of iterative method on ideone.

like image 135
Sergey Kalinichenko Avatar answered Jan 28 '26 22:01

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!