Write algorithms step by step (recursion and stack)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

All my friends who have read the previous blog know that function calling relies mainly on Stack interaction between ebp and esp. The main feature of recursion is that the function calls itself. If a function calls itself, this function is a recursive function.

 

Let's take a look at how common functions are called. If function A calls function B and function B calls Function C again, how can data in the stack be stored?

 

 

View plaincopy to clipboardprint? Function A ^

Function B | (address decrease)

Function C |

Function A ^

Function B | (address decrease)

Function C | if it is a recursive function, take a simple recursive function as an example:

 

 

View plaincopy to clipboardprint? Int iterate (int value)

{

If (value = 1)

Return 1;

Return value + iterate (value-1 );

}

Int iterate (int value)

{

If (value = 1)

Return 1;

Return value + iterate (value-1 );

} Let's call a function to see what happens?

 

 

View plaincopy to clipboardprint? Void process ()

{

Int value = iterate (6 );

}

Void process ()

{

Int value = iterate (6 );

} What is the memory stack?

 

 

View plaincopy to clipboardprint? Iterate (int 1) line 96

Iterate (int 2) line 97 + 12 bytes

Iterate (int 3) line 97 + 12 bytes

Iterate (int 4) line 97 + 12 bytes

Iterate (int 5) line 97 + 12 bytes

Iterate (int 6) line 97 + 12 bytes

Process () line 102 + 7 bytes

Main () line 108

MainCRTStartup () line 206 + 25 bytes

KERNEL32! 7c817067 ()

Iterate (int 1) line 96

Iterate (int 2) line 97 + 12 bytes

Iterate (int 3) line 97 + 12 bytes

Iterate (int 4) line 97 + 12 bytes

Iterate (int 5) line 97 + 12 bytes

Iterate (int 6) line 97 + 12 bytes

Process () line 102 + 7 bytes

Main () line 108

MainCRTStartup () line 206 + 25 bytes

KERNEL32! 7c817067 ()

As you can see, there is no difference between recursive functions and common functions. Besides calling itself, it is a common function. So when will this function be returned recursively? This is the key to recursive functions. We can see that the iterate function stops when it reaches 1, so the above stack is in (value = 1), that is, return. Therefore, the key part of a recursive function is: (1) recursive policy; (2) function exit.

 

Here, we may feel that this is not the case for recursive functions, as it is. However, we also need to keep in mind that the depth of recursion is a question we must consider. The entire recursive process is controllable only when the recursive depth is within a controllable range. When is it uncontrollable? Is the recursive depth greater than a certain number? Is this number related to the length of the thread stack? When the stack overflows, the obtained data has lost its authenticity, so it makes no sense.

 

We will promote the above question. How can we use our own defined stack to simulate the above recursive call? This not only satisfies recursive attributes, but also ensures that the function is fully controllable.

 

You can write your own solutions first. The following is just my personal idea.

 

 

View plaincopy to clipboardprint? Int iterate (int value)

{

Int count = 0;

Int number = 0;

 

Push (value );

While (-1! = (Number = pop ()))

{

If (1! = Number)

Push (number-1 );

Count + = number;

}

 

Return count;

}

Int iterate (int value)

{

Int count = 0;

Int number = 0;

 

Push (value );

While (-1! = (Number = pop ()))

{

If (1! = Number)

Push (number-1 );

Count + = number;

}

 

Return count;

}

 

 

 

 

 

 

 

[Notice: The following blog introduces algorithms and memory]

Related Article

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.