What is the difference between iteration and recursion in data structure




















It has a larger code size than recursion. The termination in iteration happens when the condition of the loop fails. In iteration, the time complexity is relatively lower than recursion. We can calculate its time complexity by finding the no. Now, let's see the comparison between iteration and recursion. We are comparing both terms based on some characteristics. JavaTpoint offers too many high quality services.

Mail us on [email protected] , to get more information about given services. Please mail your requirement at [email protected] Duration: 1 week to 2 week. Next Topic Difference between.

Reinforcement Learning. R Programming. React Native. Python Design Patterns. Python Pillow. Python Turtle. Verbal Ability. Interview Questions. Company Questions. Artificial Intelligence. Cloud Computing. Data Science. According to the Likewise, the factorial function is called again and again. Recursive functions are related with the stack. In C, the main program can have many functions. So, main is the calling function, and the function which is called by the main program is the called function.

When the function is called, the control is given to the called function. After the function execution is completed, the control is returned to main. Then the main program continues. So, it creates an activation record or a stack frame to continue execution.

In the above program, when calling factorial 3 from main, it creates an activation record in the call stack. Then, factorial 2 stack frame is created on top of the stack and so on. The activation record keeps information about local variables etc. Each time the function is called, a new set of local variables are created on the top of the stack. These stack frames can slow down the speed up. Likewise in recursion, a function calls itself.

Time complexity for a recursive function is found by the number of times, the function is called. Time complexity for one function call is O 1.

For n number of recursive calls, the time complexity is O n. Iteration is a block of instructions which repeats again and again till the given condition is true. Initialization step executes first. This step is to declare and initialize loop control variables. If the condition is true, the statements inside the curly braces execute. Those statements execute till the condition is true. After executing the statements inside the loop, the control goes to modify section.

It is to update the loop control variable. Termination condition is the condition specified in the definition of the loop. Infinite Case If base case is never reached it leads to infinite recursion leading to memory crash. If condition is never false, it leads to infinite iteration with computers CPU cycle being used repeatedly.

Memory Usage Recursion uses stack area to store the current state of the function,due to which memory usage is high.

Iteration uses the permanent storage area only for the variables involved in its code block, hence memory usage is less. Code Size Code size is comparitively smaller. Code size is comparitively larger.

Performance Since stack are is used to store and restore the state of recursive function after every function call , performance is comparitively slow. Since iteration does not have to keep re-initializing its component variables and neither has to store function states, the performance is fast. Memory Runout There is a possibility of running out of memory, since for each function call stack area gets used.



0コメント

  • 1000 / 1000