Recursive algorithm example

Source: Internet
Author: User
I. example (described in C ++): the row number Program 0 p (intw) 1 {if (w & gt; o) 2 {cout & lt; w; 3 p (w-1); 4 p (w-1); 5} 6} print the result after the statement p (4) is executed: 432112113211... "/> <scripttype =

1. example (described in C ++ ):

Row number program

0 p (int w)

1 {if (w> o)

2 {cout <

3 p (w-1 );

4 p (w-1 );

5}

6}

End

 

Print the result after executing statement p (4): 4 3 2 1 1 2 1 1 3 2 1 2 2 2 1 1

 

II. description:

1. recursive calls share the same principle as normal calls, except that each function is called by itself.

2. we can program the stack (user stack) by ourselves to implement the same functions as "recursive call.

3. during "recursive call", the system automatically sets and manages the stack (system stack ).

No need for our intervention, but this also adds the mystery of "recursive call. For better

To understand the "recursive call", the system stack is displayed as a table.

4. some descriptions of the "stack" format

Stack format:

Square
Square B
Square c
 
The row number returned after the function is called.
Called function
Value of W
 

Each time the consumer function is called, it is "in the stack" once. Once the function is executed, it is "out of the stack" once.

 

III. program explanation:

1. start to call p (4). The executed statements include: 1, 2, and 3.

End
P (4)
4
 

Execute the p (4) Statement 2: cout <

However, due to STATEMENT 3, p (3) is also called during execution. only p (3) is executed completely before p (4) can be executed ).

 

2. start to call P (3). The executed statements include: 1, 2, and 3.

4
P (3)
3
 
End
P (4)
4
 

When p (3) is executed, statement 4 in p (4) is executed (therefore, "4" is entered in square ").

Execute the p (3) Statement 2: cout <

In the same case as above, when Statement 3 is executed, p (2) is also called. only p (2) is executed completely before p (3) can be executed ).

 

3. start to call P (2). The executed statements include: 1, 2, and 3.

4
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

Execute the p (2) Statement 2: cout <

In the same case as above, when Statement 3 is executed, p (1) is also called. only p (1) is executed completely before p (2) can be executed ).

 

4. start to call P (1). The executed statements include: 1, 2, and 3.

4
P (1)
1
 
4
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

Execute the p (2) Statement 2: cout <

In the same case as above, when Statement 3 is executed, p (0) is also called. the execution of p (1) can continue only after p (0) is executed ).

 

5. start to call P (0). The executed statements include: 1.

4
P (0)
0
 
4
P (1)
1
 
4
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

Because w = 0 does not meet statement 1, jump directly to statement 5 and 6, so that p (0) is executed completely, and p (0) needs to perform the "Out Stack" operation.

 

6. the statements executed at this time are: 4

4
P (1)
1
 
4
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

Since p (0) execution is complete, and p (0) is 4 in square a, so continue to execute p (1) statement 4: p (w-1 ); because the w value in p (1) square c is 1, p (0) is called ).

7. start calling p (0)

5
P (0)
0
 
4
P (1)
1
 
4
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

When p (0) is executed, statement 5 in p (1) is executed (so "5" is entered in square ").

Because w = 0 does not meet statement 1, jump directly to statement 5 and 6, so that p (0) is executed completely, and p (0) needs to perform the "Out Stack" operation.

 

8. the statements executed at this time are: 5

4
P (1)
1
 
4
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

Since p (0) is executed completely, and p (0) is 5 in square a, so continue to execute p (1) statement 5 (last sentence), so p (1) after the execution is complete, p (1) needs to perform the "Out Stack" operation.

 

9.

4
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

Since p (1) execution is complete and p (1)'s square a is 4, continue to execute the p (2) statement 4: p (w-1 ); because the w value in p (2) square c is 2, p (1) is called ).

 

10. start to call P (1). The executed statements include: 1, 2, and 3.

5
P (1)
1
 
4
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

When p (1) is executed, statement 5 in p (2) is executed (so "5" is entered in square ").

Execute statement 2 of p (1): cout <

When statement 3 is executed, p (0) is also called. the execution of p (1) can continue only after p (0) is executed ).

 

11. start to call P (0). The statements executed at this time are: 1

4
P (0)
0
 
5
P (1)
1
 
4
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

Because w = 0 does not meet statement 1, jump directly to statement 5 and 6, so that p (0) is executed completely, and p (0) needs to perform the "Out Stack" operation.

 

12. the statements executed at this time are: 4

5
P (1)
1
 
4
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

Since p (0) execution is complete, and p (0) is 4 in square a, so continue to execute p (1) statement 4: p (w-1 ); because the w value in p (1) square c is 1, p (0) is called ).

 

13. start to call p (0)

5
P (0)
0
 
5
P (1)
1
 
4
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

When p (0) is executed, statement 5 in p (1) is executed (so "5" is entered in square ").

Because w = 0 does not meet statement 1, jump directly to statement 5 and 6, so that p (0) is executed completely, and p (0) needs to perform the "Out Stack" operation.

14. the statements executed at this time are: 5

5
P (1)
1
 
4
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

Since p (0) is executed completely, and p (0) is 5 in square a, so continue to execute p (1) statement 5 (last sentence), so p (1) after the execution is complete, p (1) needs to perform the "Out Stack" operation.

 

15.

4
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

Since p (1) is executed completely and p (1) is 5 in square a, so continue to execute p (2) statement 5 (last sentence), so p (2) after the execution is complete, p (2) needs to perform the "Out Stack" operation.

 

Note: In fact, step 10 ~ 15. Repeat Steps 4 ~ 9, because all of them call P (1)

 

16.

4
P (3)
3
 
End
P (4)
4
 

Since p (2) execution is complete and p (2)'s square a is 4, continue to execute the p (3) statement 4: p (w-1 ); because the w value in p (3) square c is 3, p (2) is called ).

 

17. start to call P (2). The executed statements include: 1, 2, and 3.

5
P (2)
2
 
4
P (3)
3
 
End
P (4)
4
 

When p (2) is executed, statement 5 in p (3) is executed (so "5" is entered in square ").

Execute the p (2) Statement 2: cout <

 

In the same case as above, when Statement 3 is executed, p (1) is also called. only p (1) is executed completely before p (2) can be executed ).

 

18. start calling p (1)

Omitted ......

Note: In fact, steps 17 ~ 29 repeated 3 ~ 15, because all of them call P (2)

In this step, 2 1 1 is printed (see steps 3, 4, 10)

 

IV. Conclusion and analysis:

Procedure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
Result
4
3
2
1
1
2
1
1
3
2
1
1
2
1
1
 

5th results repeat 4th results because all of them call p (1)

6th, 7, and 8 results repeat 3rd, 4, and 5 results because they all call p (2)

9th ~ The 15 results are repeated by 2nd ~ Eight results. this is because all of them call p (3)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.