|
|
1234 1243 1324 1342 1432 1423 2134 2143 2314 2341 2431 2413 3214 3241 3124 3142 3412 3421 4231 4213 4321 4312 4132 4123 |
1234 1243 1324 1342 1432 1423 |
1234 1243 |
|
|
|
|
I need a simple break-down. |
Therefore we go back to the beginning of the function to reset all the parameters and variables with the new arguments passed |
wizebin wrote: |
---|
2. Your code will NEVER break out of a for loop without your explicit instruction (few exceptions, including exceptions and force) |
if (i == n)
this then executes line 27 and the function ends.Here is your misunderstanding. When you call a function recursively in C++, the second call gets a new set of parameters and local variables |
|
|
FIRST SWAP DONE! - i = 0 ---- j = 0 FIRST SWAP DONE! - i = 1 ---- j = 1 FIRST SWAP DONE! - i = 2 ---- j = 2 1234 SECOND SWAP DONE!- i = 2 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 3 1243 SECOND SWAP DONE!- i = 2 ---- j = 3 SECOND SWAP DONE!- i = 1 ---- j = 1 FIRST SWAP DONE! - i = 1 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 2 1324 SECOND SWAP DONE!- i = 2 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 3 1342 SECOND SWAP DONE!- i = 2 ---- j = 3 SECOND SWAP DONE!- i = 1 ---- j = 2 FIRST SWAP DONE! - i = 1 ---- j = 3 FIRST SWAP DONE! - i = 2 ---- j = 2 1432 SECOND SWAP DONE!- i = 2 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 3 1423 SECOND SWAP DONE!- i = 2 ---- j = 3 SECOND SWAP DONE!- i = 1 ---- j = 3 SECOND SWAP DONE!- i = 0 ---- j = 0 FIRST SWAP DONE! - i = 0 ---- j = 1 FIRST SWAP DONE! - i = 1 ---- j = 1 FIRST SWAP DONE! - i = 2 ---- j = 2 2134 SECOND SWAP DONE!- i = 2 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 3 2143 SECOND SWAP DONE!- i = 2 ---- j = 3 SECOND SWAP DONE!- i = 1 ---- j = 1 FIRST SWAP DONE! - i = 1 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 2 2314 SECOND SWAP DONE!- i = 2 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 3 2341 SECOND SWAP DONE!- i = 2 ---- j = 3 SECOND SWAP DONE!- i = 1 ---- j = 2 FIRST SWAP DONE! - i = 1 ---- j = 3 FIRST SWAP DONE! - i = 2 ---- j = 2 2431 SECOND SWAP DONE!- i = 2 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 3 2413 SECOND SWAP DONE!- i = 2 ---- j = 3 SECOND SWAP DONE!- i = 1 ---- j = 3 SECOND SWAP DONE!- i = 0 ---- j = 1 FIRST SWAP DONE! - i = 0 ---- j = 2 FIRST SWAP DONE! - i = 1 ---- j = 1 FIRST SWAP DONE! - i = 2 ---- j = 2 3214 SECOND SWAP DONE!- i = 2 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 3 3241 SECOND SWAP DONE!- i = 2 ---- j = 3 SECOND SWAP DONE!- i = 1 ---- j = 1 FIRST SWAP DONE! - i = 1 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 2 3124 SECOND SWAP DONE!- i = 2 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 3 3142 SECOND SWAP DONE!- i = 2 ---- j = 3 SECOND SWAP DONE!- i = 1 ---- j = 2 FIRST SWAP DONE! - i = 1 ---- j = 3 FIRST SWAP DONE! - i = 2 ---- j = 2 3412 SECOND SWAP DONE!- i = 2 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 3 3421 SECOND SWAP DONE!- i = 2 ---- j = 3 SECOND SWAP DONE!- i = 1 ---- j = 3 SECOND SWAP DONE!- i = 0 ---- j = 2 FIRST SWAP DONE! - i = 0 ---- j = 3 FIRST SWAP DONE! - i = 1 ---- j = 1 FIRST SWAP DONE! - i = 2 ---- j = 2 4231 SECOND SWAP DONE!- i = 2 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 3 4213 SECOND SWAP DONE!- i = 2 ---- j = 3 SECOND SWAP DONE!- i = 1 ---- j = 1 FIRST SWAP DONE! - i = 1 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 2 4321 SECOND SWAP DONE!- i = 2 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 3 4312 SECOND SWAP DONE!- i = 2 ---- j = 3 SECOND SWAP DONE!- i = 1 ---- j = 2 FIRST SWAP DONE! - i = 1 ---- j = 3 FIRST SWAP DONE! - i = 2 ---- j = 2 4132 SECOND SWAP DONE!- i = 2 ---- j = 2 FIRST SWAP DONE! - i = 2 ---- j = 3 4123 SECOND SWAP DONE!- i = 2 ---- j = 3 SECOND SWAP DONE!- i = 1 ---- j = 3 SECOND SWAP DONE!- i = 0 ---- j = 3 |
I was doing fine until the first "1=2 ---- j = 3". Then I got lost as to how "1243" is printed. I understand how the 4 and 3 are swapped, but how does the cout statement execute? |
permute(a,3,3)
. This call hits the base case at line 19 so it prints 1243 at line 20 and exits.
|
|
Entering permute(1234, 0, 3) Entering permute(1234, 1, 3) Entering permute(1234, 2, 3) Entering permute(1234, 3, 3) 1234 Leaving permute(1234, 3, 3) Entering permute(1243, 3, 3) 1243 Leaving permute(1243, 3, 3) Leaving permute(1234, 2, 3) Entering permute(1324, 2, 3) Entering permute(1324, 3, 3) 1324 Leaving permute(1324, 3, 3) Entering permute(1342, 3, 3) 1342 Leaving permute(1342, 3, 3) Leaving permute(1324, 2, 3) Entering permute(1432, 2, 3) Entering permute(1432, 3, 3) 1432 Leaving permute(1432, 3, 3) Entering permute(1423, 3, 3) 1423 Leaving permute(1423, 3, 3) Leaving permute(1432, 2, 3) Leaving permute(1234, 1, 3) Entering permute(2134, 1, 3) Entering permute(2134, 2, 3) Entering permute(2134, 3, 3) 2134 Leaving permute(2134, 3, 3) Entering permute(2143, 3, 3) 2143 Leaving permute(2143, 3, 3) Leaving permute(2134, 2, 3) Entering permute(2314, 2, 3) Entering permute(2314, 3, 3) 2314 Leaving permute(2314, 3, 3) Entering permute(2341, 3, 3) 2341 Leaving permute(2341, 3, 3) Leaving permute(2314, 2, 3) Entering permute(2431, 2, 3) Entering permute(2431, 3, 3) 2431 Leaving permute(2431, 3, 3) Entering permute(2413, 3, 3) 2413 Leaving permute(2413, 3, 3) Leaving permute(2431, 2, 3) Leaving permute(2134, 1, 3) Entering permute(3214, 1, 3) Entering permute(3214, 2, 3) Entering permute(3214, 3, 3) 3214 Leaving permute(3214, 3, 3) Entering permute(3241, 3, 3) 3241 Leaving permute(3241, 3, 3) Leaving permute(3214, 2, 3) Entering permute(3124, 2, 3) Entering permute(3124, 3, 3) 3124 Leaving permute(3124, 3, 3) Entering permute(3142, 3, 3) 3142 Leaving permute(3142, 3, 3) Leaving permute(3124, 2, 3) Entering permute(3412, 2, 3) Entering permute(3412, 3, 3) 3412 Leaving permute(3412, 3, 3) Entering permute(3421, 3, 3) 3421 Leaving permute(3421, 3, 3) Leaving permute(3412, 2, 3) Leaving permute(3214, 1, 3) Entering permute(4231, 1, 3) Entering permute(4231, 2, 3) Entering permute(4231, 3, 3) 4231 Leaving permute(4231, 3, 3) Entering permute(4213, 3, 3) 4213 Leaving permute(4213, 3, 3) Leaving permute(4231, 2, 3) Entering permute(4321, 2, 3) Entering permute(4321, 3, 3) 4321 Leaving permute(4321, 3, 3) Entering permute(4312, 3, 3) 4312 Leaving permute(4312, 3, 3) Leaving permute(4321, 2, 3) Entering permute(4132, 2, 3) Entering permute(4132, 3, 3) 4132 Leaving permute(4132, 3, 3) Entering permute(4123, 3, 3) 4123 Leaving permute(4123, 3, 3) Leaving permute(4132, 2, 3) Leaving permute(4231, 1, 3) Leaving permute(1234, 0, 3) |