Thursday, December 16, 2010

Difference between Recursion and Iteration

1) Recursive function – is a function that is partially defined by itself whereas Iterative functions – are loop based imperative repetitions of a process

2)
Recursion Uses selection structure whereas Iteration uses repitation structure

3)
An infinite loop occurs with iteration if the loop-continuation test never becomes false whereas infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on the base case.

4)
Iteration terminates when the loop-continuation condition fails whereas recursion terminates when a base case is recognized

5)
When using recursion multiple activation records are created on stack for each call when in iteration everything is done in one activation record

6) Recursion is usually slower then iteration due to overhead of maintaining stack whereas iteration does not use stack so it's faster than recursion

7) Recursion uses more memory than iteration

8) Infinite recursion can crash the system whereas infinite looping uses cpu
cycles repeatedly

9) Recursion makes code smaller and iteration makes code longer

24 comments:

  1. Replies
    1. Indeed great compilation, worth looking. By the way iteration is the way to go in production because deep recursion can blow Stack. here is a program to reverse String with recursion and iteration which help to understand both.

      Delete
    2. You are right about recursion. Many times I got error in python that says "Maximum stack limit is reached" and program stops.

      Delete
  2. Thanks a lot...
    Best answer...

    ReplyDelete
  3. got an xam 2day and dis was very very helpful.

    ReplyDelete
    Replies
    1. Pleased to know that article was helpful to you.

      Delete
    2. me to having exam tomorow

      Delete
  4. thks........... nd help fr exms.......

    ReplyDelete
  5. Great effort, man. Got an exam tomorrow. You were of immense help.

    ReplyDelete
    Replies
    1. Thanks Soham. Glad to know that article was helpful to you man.

      Delete
  6. thanks
    and will u please tell me
    which one is most preparable
    justify it

    ReplyDelete
    Replies
    1. IMHO it depends on situation. Recursion creates lots of stack frames. In some programming languages no of stack frames that can be created, are limited. So iteration is preferable.

      Delete
  7. thank you very much. I really needed it

    ReplyDelete
  8. Thank u very much!! You just saved my assignment marks :-p lol!

    ReplyDelete
  9. thanq very much useful!!

    ReplyDelete
  10. thank u ,, it is very useful to me in today's condition

    ReplyDelete
  11. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Data Science with Python , kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Sangita Mohanty
    MaxMunus
    E-mail: sangita@maxmunus.com
    Skype id: training_maxmunus
    Ph:(0) 9738075708 / 080 - 41103383
    http://www.maxmunus.com/

    ReplyDelete
  12. Well explained string operation .
    there are good String program collection visit Top String program

    ReplyDelete
  13. I’m impressed, I must say. Seldom do I come across a blog that’s both educative and interesting, and without a doubt, you have hit the nail on the head. The problem is an issue that not enough folks are speaking intelligently about. Now i'm very happy I stumbled across this during my hunt for something regarding this.
    tnmachi

    ReplyDelete

Note: Only a member of this blog may post a comment.