Step-by-step write algorithm (loops and recursion)

Source: Internet
Author: User

Text: Step-by-step write algorithm (loop and recursion)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


In fact, programming friends know that no matter what language you learn, loops and recursion are two things that must be learned. Of course, if the loop is good to understand a bit, then recursion is not that simple. We used to be secretive about recursion, but what I want to tell you is that recursion isn't really that scary. The so-called recursion is the function itself calls itself, the loop is essentially a recursive.

1) summation recursive function

We can give an example of a loop, as we said earlier, if you write a sum function of 1 to n, you might write:

int calculate (int m) {int count = 0;if (M <0) return-1;for (int index = 0; index <= m; index++) Count + = Index;return Co UNT;}
The above is just a demonstration. Let's see if recursion should be written.

int calculate (int m) {if (M = = 0) return 0;elsereturn calculate (m-1) + M;}
What's the difference between the two pieces of code?

(1) The first paragraph of code from 0, began to calculate, from 0 to M gradually calculated; the second code is calculated from 10, gradually to 0 after this time, so that the same can achieve the effect of the calculation

(2) The first paragraph of the code does not need to repeat the stack operation, the second paragraph needs to repeat the function operation, of course, this is the nature of recursion

(3) The first paragraph of code is longer, the second section of the code is relatively short


2) Finding recursive functions

You might say that the code is a bit special. If you are looking for a function of a class, is it possible to modify it into a recursive function?

int find (int array[], int length, int value) {int index = 0;if (NULL = = Array | | 0 = = length) return-1;for (; index < Leng Th index++) {if (value = = Array[index]) return index; return-1;}

You might say that this code might be modified to code like this:

int _find (int index, int array[], int length, int value) {if (index = = length) return-1;if (value = = Array[index]) return Inde X;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, Valu e);}

3) pointer variable traversal

Structure pointers are the traversal structures we like, just imagine if there is a data structure defined below:

typedef struct _NODE{INT data;struct _node* next;} NODE;
So, what do we do when we need to print all the data in a node link? You can think about it first and see if we write the code right.

void print (const node* pnode) {if (NULL = = Pnode) return;while (pnode) {printf ("%d\n", pnode->data);p NODE = pnode-> Next;}}
Then if you change to recursion, it's simpler:

void print (const node* pnode) {if (NULL = = pnode) return;else    printf ("%d\n", Pnode->data);p rint (pnode->next);}

In fact, write so much, just want to share my personal point of view: The loop is a special recursion, only the recursion and the stack is equivalent. All of the recursive code can be written in the form of a stack, the following blog we will discuss the stack and recursive relationship. To write well, you have to master the stack.



"Trailer: The following blog introduces stacks and recursion"

Step-by-step write algorithm (loops and recursion)

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.