Is writing recursive functions hard for you?

Posted by null on Programmers See other posts from Programmers or by null
Published on 2012-06-05T16:28:16Z Indexed on 2012/06/05 16:47 UTC
Read the original article Hit count: 242

I'm taking a computer science course now. I feel it is so hard compared to my polytechnic IT course. I can't understand recursive methods well. How do you manage to get your brain to understand it? When the recursive method returns back, my brain can not trace it nicely.

Is there a better way to understand recursion? How do you start learning it? Is it normal that feel that it is very hard at then beginning?

Look at the example, I'm very surprised how can I write this if it appears in exam.

    public class Permute {

    public static void main(String[] args) {
        new Permute().printPerms(3);
    }

    boolean[] used;
    int max;

    public void printPerms(int size) {
        used = new boolean[size];
        max = size;
        for (int i = 0; i < size; i++) {
            used[i] = false;
        }
        perms(size, "");
    }

    public void perms(int remaining, String res) {
        if (remaining == 0) {
            System.out.println(res);
        } else {
            for (int i = 0; i < max; i++) {
                if (!(used[i])) {
                    used[i] = true;
                    perms(remaining - 1, res + " " + i);
                    used[i] = false;
                }
            }
        }
    }
}

© Programmers or respective owner

Related posts about learning

Related posts about methodology