Write algorithms step by step (loop and Recursion)

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]

 


Actually, programming friends know that loop and Recursion are two things that must be learned no matter what language they learn. Of course, if the loop is better understood, recursion is not that simple. We have never been so familiar with recursion, but what I want to tell you is that recursion is actually not so terrible. The so-called recursion means that the function calls itself, and the loop is actually a recursion.

1) sum recursion Function

We can give an example of a loop. As we mentioned earlier, if you write a sum function of 1 to n, you may write it like this:


Int calculate (int m)
{
Int count = 0;
If (m <0)
Return-1;
 
For (int index = 0; index <= m; index ++)
Count + = index;

Return count;
}
Int calculate (int m)
{
Int count = 0;
If (m <0)
Return-1;

For (int index = 0; index <= m; index ++)
Count + = index;
 
Return count;
} The above is just a demonstration. Next let's take a look at how to write recursion?


Int calculate (int m)
{
If (m = 0)
Return 0;
Else
Return calculate (m-1) + m;
}
Int calculate (int m)
{
If (m = 0)
Return 0;
Else
Return calculate (m-1) + m;
} What are the differences between the two codes?

(1) The first code starts to be calculated from 0, and starts to be calculated from 0 to m. The second code starts to be calculated from 10, and then ends to 0, in this way, the computing results can also be achieved.

(2) The first Code does not require repeated stack operations, and the second code requires repeated function operations. Of course, this is also the essence of recursion.

(3) The first code is relatively long, and the second code is short.

 


2) Search for recursive functions

You may say that these codes are special. Is it possible to change a function of the Search Class to a recursive function?


Int find (int array [], int length, int value)
{
Int index = 0;
If (NULL = array | 0 = length)
Return-1;
 
For (; index <length; index ++)
{
If (value = array [index])
Return index;
}
 
Return-1;
}
Int find (int array [], int length, int value)
{
Int index = 0;
If (NULL = array | 0 = length)
Return-1;

For (; index <length; index ++)
{
If (value = array [index])
Return index;
}

Return-1;
}

You may say that such code may be changed to such code:


Int _ find (int index, int array [], int length, int value)
{
If (index = length)
Return-1;
 
If (value = array [index])
Return index;
 
Return _ find (index + 1, array, length, value );
}
 
Int find (int array [], int length, int value)
{
If (NULL = array | length = 0)
Return-1;
 
Return _ find (0, array, length, value );
}
Int _ find (int index, int array [], int length, int value)
{
If (index = length)
Return-1;

If (value = array [index])
Return index;

Return _ find (index + 1, array, length, value );
}

Int find (int array [], int length, int value)
{
If (NULL = array | length = 0)
Return-1;

Return _ find (0, array, length, value );
}

3) pointer variable Traversal

The structure pointer is our favorite traversal structure. Imagine if there is a data structure defined below:


Typedef struct _ NODE
{
Int data;
Struct _ NODE * next;
} NODE;
Typedef struct _ NODE
{
Int data;
Struct _ NODE * next;
} NODE; then, we need to print all the data in a NODE link. What should we do? You can first think about it and then check whether the code we wrote is correct.


Void print (const NODE * pNode)
{
If (NULL = pNode)
Return;
 
While (pNode ){
Printf ("% d \ n", pNode-> data );
PNode = pNode-> next;
}
}
Void print (const NODE * pNode)
{
If (NULL = pNode)
Return;

While (pNode ){
Printf ("% d \ n", pNode-> data );
PNode = pNode-> next;
}
}
Now, if it is changed to recursion, it will be simpler:


Void print (const NODE * pNode)
{
If (NULL = pNode)
Return;
Else
Printf ("% d \ n", pNode-> data );

Print (pNode-> next );
}
Void print (const NODE * pNode)
{
If (NULL = pNode)
Return;
Else
Printf ("% d \ n", pNode-> data );
 
Print (pNode-> next );
}
As a matter of fact, I want to share with you my personal point of view that loop is a special recursion, and only recursion and stack are equivalent. All recursive code can be written as a stack. The following blog will discuss the relationship between stack and recursion. To write well, you must be familiar with the stack.

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.