You are not logged in. Please login at www.codechef.com to post your questions!

×

Recursion Vs Looping

I recently heard from my friend that recursion is more efficient than the normal method of iteration. But being a newbie with my limited knowledge i cant believe that. Is that the real fact? Can any one please help me out?

This question is marked "community wiki".

asked 19 Nov '13, 22:29

bipin2's gravatar image

3★bipin2
3.1k254671
accept rate: 8%

edited 19 Nov '13, 23:00


@bipin2 Recursion is easy to implement but Looping way (Iterative Approach) is always better to use.

There are several reasons 1 is Recursion may cause Stack Overflow problem If you have not implemented it correctly , Forgot to use base cases and termination cases for recursion code. And more , it is very difficult to debug if you have several Recursive calls in you code.

So always prefer iterative approach while coding .. for some algorithm iterative is also difficult to implement so we can't say exactly that which is better..

link

answered 19 Nov '13, 23:01

upendra1234's gravatar image

2★upendra1234
2.3k183069
accept rate: 1%

Hi,

Iteration is generally more efficient except in the cases of functional programming languages such as LISP. But undoubtedly, most of the problems are easier to solve by recursion, hence recursion techniques are used mostly where efficiency is a secondary issue.

link

answered 19 Nov '13, 23:04

saurabhgarg's gravatar image

3★saurabhgarg
15634
accept rate: 16%

thanks bro!!!

(19 Nov '13, 23:24) bipin23★

Successive recursive function calls use stack to execute them.

In the case of recursion, all the recursive function calls are stored on the stack to return control back to the caller functions poping the recursive calls, therefore making recursion slower than looping, because of these overheads.

But there are some algorithms that will be very difficult to implement if not done recursively, like many tree traversal algorithms, graph traversals like dfs, bfs, etc.

So, the bottom line is that recursion makes problem solving easy in many complex algorithms, which makes the overheads involved in recursive function call not that important when comparing them with the complexity of the problem itself.

link

answered 19 Nov '13, 23:20

pratku123's gravatar image

4★pratku123
1.8k4933
accept rate: 14%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×348
×191

question asked: 19 Nov '13, 22:29

question was seen: 1,082 times

last updated: 19 Nov '13, 23:24