Read answer of question: "What is recursion?"
{
if(base condition)
{
return some finite value.
}
else
{
return func1(input2) //Function calls itself.
}
}
So, to design a solution(algorithm) to any problem in such a way that one function is having a call to itself inside a body and problem is reduced in each successive self calls to that limit that we reach base condition that returns some result(finite value) and no further recursive calls are made further. This makes a recursive solution and this way of solving the problem is called 'Recursion'. Examples of recursive problems:1.Check given string is a palindrome. E.g. 'RACECAR'.
2.Factorial of number.
3.Calculating nth term of 'Fibonnaci' series. E.g. if n = 4. Then series is 0,1,1,2,3
4.Binary Search
5.Greatest Common Divisor (GCD) calculation. Palindrome Recursive Algorithmstatic int depth; //GLOBAL CLASS VARIABLE
public bool IsPalindrome(string PalindromeString)
{
depth++;//STANDARD
if (depth PalindromeString[PalindromeString.Length - 1])//STANDARD - STOP CONDITION BASED ON CURRENT DATA MANIPULATION
{
//last result is propogated to last uppermost level of depth = 1 without modification. At last depth we are at the result as the we have already reduced the problem so that last level result becomes the original result.
//string is palindrome if current first and last characters are equal and substring left after taking first and last character is a palindrome.
PalindromeString = PalindromeString.Substring(1, PalindromeString.Length - 2);//STANDARD DATA MANIPULATION CONDITION IN FORM OF f(n) processing done on data = same processing done on f(n-1) * some post processing.
return IsPalindrome(PalindromeString); //STANDARD - RECURSION CALL BASED ON ELSE OF STOP CONDITION OR IF PRE PRE PROCESSING IS DONE THROUGH DATA MANIPULATION STOP CONDITION THEN BASED ON ITS IF OR ELSE CLAUSE.
}
else
return false;
}
}
Happy Programming :-)
Himanshu Singh
-noun Mathematics, Computers .
the process of defining a function or calculating a number by the repeated application of an algorithm.
In computer programming, a function can call another function. If a function calls itself, it is said to be recursive. Doing this correctly can it quite simple to solve certain problems, that otherwise look very complicated.
In math, a function is defined as recursive when it is defined in terms of itself. For example, the factorial of a number is the product of all numbers up to this number. Thus, the factorial of 4, written 4!, is equal to 1 x 2 x 3 x 4. A common definition for this function is as follows: 0! = 1, for all numbers "n" greater than zero, n! = n x (n-1)! For example, the factorial of 4 (which is 4 x 3 x 2 x 1) is equal to 4 times the factorial of 3 (which is 3 x 2 x 1).
Assuming you mean "recursion", recursion is a function calling itself.
A good example of when you would use recursion is for a directory walker. You would call the function, passing it a directory path. The function would call itself for all the directories in the directory you passed in. It would then do whatever processing it needed to on all the files in the directory.
To delete a linked list walk through the list and delete the memory allocated to each element, remembering the next element address, and then iterating or recursing the process using the next element address, until the next element address is null.
the function will be int sum_num(int num) { int p = 0, y; y = num; while (y != 0) { p = (y % 10) + p; y = y/10; } return p; } if you can help me with recursive function i would appreciate it Here it is - int SumOfDigits(int input) { int sum = 0; /// Compute the sum while(input > 0) { sum += input %10; input /= 10; /// print value for examination printf(" %d %d\n",sum, input); } return sum; } int RecursiveSumOfDigits(int input) { /// Deal with the simplest case if(input < 10) return input; printf("Recursing on %d\n",input); return RecursiveSumOfDigits(SumOfDigits(input)); }
1- set pass =1 2- repeat step 3 varying j from 0 to n-1-pass 3- if the element at index j is >than the element at index j+1 swap the two element 4- increment pass by 1 5-if pass is <=n-1 go to step 2
Quicksort is a recursive algorithm. Given an array and indices to the lower and upper bounds of a subset within the array, divide the subset into two subsets around a pivot value then recursively apply the algorithm to each of these subsets until a subset has fewer than 2 elements. The algorithm begins by checking the size of the given subset. If there are fewer than 2 elements then the subset is sorted. This represents the end condition for this instance of the algorithm since a subset of 0 or 1 elements can always be regarded as being sorted. If the set has 2 or more elements, however, a pivot element is selected . the pivot is typically the mode of the first, last and middle elements. The pivot is then swapped with the last element so it's out of the way. Two indices are then instantiated, left and right, one referring to the first element (the lower bound), the other referring to the penultimate element (we ignore the pivot at the end for now). While the left index is less than the right index, repeat: 1. While left is less than right and the value referred to by left is less than the pivot, increment left. (In other words, search for the first value from the left but no further than the right that is not less than the pivot). 2. While right is greater than left and the value referred to by right is not less than the pivot, decrement right. (In other words, search for the first value from the right but no further than the left index that is less than the pivot). 3. If the left and right indices are not equal, swap the values at those indices. When the indexes are the same, or left crosses right, the loop ends. Swap the value at the left index with the last element (the pivot). At this point, the pivot is now in place and all values less than the pivot are to its left, with all others to its right. The values on either side are not yet sorted, thus we recursively invoke the same algorithm to sort each of these two subsets (excluding the pivot, of course). Since the set is divided into two smaller subsets upon each invocation of the algorithm, each recursion takes less time because there are fewer elements to divide. The recursions effectively form a binary tree with one recursion per node. Some nodes will terminate earlier than others so the tree will probably be unbalanced. But since the sorting is done in-place, each recursion will unwind automatically leaving behind a sorted subset within the set. When the initial instance of the algorithm terminates, the entire set is sorted.