answersLogoWhite

0

Iterative: assume that the first element in a list is the maximum (assign it to a variable "maximum"). Use a loop to compare it to the second, third, etc. element, up to the last one. Every time you find an element that is larger than the current maximum, assign it to maximum.

Recursive: If the list has a single element, this is the maximum. Otherwise, divide the list into two shorter sub-lists. Get the maximum of each of the shorter sub-lists. Compare them, and use a simple "if" to return the larger of the two.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

How do you choose between recursion and iteration?

Some problems cry out for recursion. For example, an algorithm might be defined recursively (e.g. the Fibonacci function). When an algorithm is given with a recursive definition, the recursive implementation is straight-forward. However, it can be shown that all recursive implementations have an iterative functional equivalent, and vice versa. Systems requiring maximum processing speed, or requiring execution within very limited resources (for example, limited stack depth), are generally better implemented using iteration.


What are the advantages of using a recursive function in c?

Advantages:Through Recursion one can Solve problems in easy way whileits iterative solution is very big and complex.Ex : tower of HanoiYou reduce size of the code when you use recursive call.Disadvantages :Recursive solution is always logical and it is verydifficult to trace.(debug and understand)Before each recursive calls current values of the variblesin the function is stored in the PCB, ie process controlblock and this PCB is pushed in the OS Stack.So sometimes alot of free memory is require for recursivesolutions.Remember : whatever could be done through recursion could bedone through iterative way but reverse is not true.


What is the maximum depth of recursive calls a function may make?

Recursive function call depend your primary memory space because the recursive call store in stack and stack based on memory.


What is the algorithm for factorial using recursion in c pgm?

int nfact (int N) if (N < 2) return 2 else return nfact (N - 1); However; this algorithm fails miserably for even small values of N1 due to arithmetic overflow. For arbitrary values of N, you need an arbitrary length math library, many of which are iterative, not recursive. ------------------------------------------------------------------------ 1In 32 bit notation, the maximum N is 12. In 64 bit notation, the maximum N is 20.


What is iteration in c programming?

Iteration means what it says: the act of repeating a process. Each repetition of the process is itself an iteration, and the results of one iteration can be applied in the next iteration. Counting from 1 to 10 is an example of an iterative process because each iteration increments the counter by 1. Iteration should not be confused with recursion. Although similar, a recursion occurs when a function calls itself. Such functions are known as recursive functions and these make use of the call stack to remember the "state" of each function prior to the next call, thus allowing those states to be restored when the recursive calls return (known as "unwinding"). Since the call stack must be maintained regardless of the depth of calls, recursive routines that do not need to remember the state of each recursion are inefficient, and are often better implemented as iterative loops. However, this may require nested iterations and, if the depth is too variable or complex to be calculated at compile time, or the maximum depth would be greater than 16 then the cost of recursion will often be preferred to the increased code size iteration would incur. Even so, recursive functions can often be inline expanded by the compiler as iterative functions, thus simplifying the source code without sacrificing the performance.


Why does the recursive implementation of quicksort require O(log n) of additional space?

The recursive implementation of quicksort requires O(log n) of additional space because it uses the call stack to keep track of subarrays being sorted. Each recursive call adds a new frame to the call stack, and the maximum depth of the call stack is O(log n) in the average case.


How to calculate the height of a binary tree?

To calculate the height of a binary tree, you can use a recursive algorithm that finds the maximum height of the left and right subtrees, and then adds 1 to the maximum height. This process is repeated for each node in the tree until the height of the entire tree is calculated.


Find the maximum elements in a finite sequence?

I think this man ewkajf


What is the maximum number of electrons found in any shell of the known elements?

A nucleus can have up to seven shells and the maximum each shell can hold is 2N^2. The maximum number of electrons found in any shell of known elements is 32.


If A and B are two sets containing 3 or 6 elements respectively what are the minimum and the maximum number of elements in AUB?

Minimum 6 (the size of the larger of the two sets); maximum 9 (the sum of the sizes).


What is the maximum sum that can be achieved by selecting non-adjacent elements from a given list of numbers?

To find the maximum sum by selecting non-adjacent elements from a list of numbers, you can use dynamic programming. Start by creating an array to store the maximum sum up to each element. Iterate through the list of numbers and for each element, calculate the maximum sum by either including the current element or excluding it. Keep track of the maximum sum achieved so far. At the end of the iteration, the final element in the array will contain the maximum sum that can be achieved by selecting non-adjacent elements.


What elements completes the 2s sublevel?

The 2s sublevel can hold a maximum of 2 electrons.