I got the code from this question, I ran it in Eclipse and the code was fine, but I confused myself in how the recursion order goes internally.
public class Permute {
public static void main(String[] args) throws IOException {
System.out.println("Enter a string");
BufferedReader bufReader = new BufferedReader(new InputStreamReader(
System.in));
String text = bufReader.readLine();
shuffle("", text);
}
public static void shuffle(String dummy, String input) {
if (input.length() <= 1)
System.out.println(dummy + input);
else {
for (int i = 0; i < input.length(); i++) {
input = input.substring(i, i + 1) + input.substring(0, i)
+ input.substring(i + 1);
shuffle(dummy + input.substring(0, 1), input.substring(1));
}
}
}
}
I found difficulty in understanding recursion in the for
loop of Shuffle
. Any pointers in decoding the recursion steps?
EDIT : Okay this is my understanding, say suppose my input is ABC and when i run in the first loop , i get dummy = A and input = BC, so the immediate step would be to go down the recursion for input = BC and dummy = A and then come back to iterate i for the initial input ?