Recursive Algorithm example

Source: Internet
Author: User

1. Example (described in C ++ ):

Row number Program

0 p (int w)

1 {if (w> o)

2 {cout <w;

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 <w; print 4 (this is the first result)

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 <w; print 3 (this is the second result)

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 <w; print 2 (this is the third result)

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 Statement 2 of p (2): cout <w; print 1 (this is the fourth result)

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 the p (1) Statement 2: cout <w; print 1 (this is the fifth result)

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 <w; print 2 (this is the sixth result)

 

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.