I have been revising algorithms for some days. Actually , i am not aware that iterative versions of the algorithms do exist but why they are not popular as their recursive counterparts.
Even the Arrays.sort method in java implements recursive quick sort.I heard that recursion causes overhead in maintaining the stack frames for each method call.
for e.g: Iterative version for merge sort is even simple. It first sort the adjacent elements and then the sorted pairs , so on.
I posted this question on StackOverflow too: but dint get a convincing answer.What are your views about this??
Thanks,
S.Hareendra
Tim Moores
Rancher
Joined: Sep 21, 2011
Posts: 2329
posted
0
Quick sort (one of the fastest sorting algorithm) is recursive; trying to implement it in iterative fashion will lead to hard-to-understand code.
I heard that recursion causes overhead in maintaining the stack frames for each method call.
Yes, there is an overhead, but there is an overhead in implementing an iterative version, too. Recursion by itself is not something to be afraid of.
memory is pretty cheap these days. My father used to tell me about when they paid basically $1 PER BYTE of memory. Today, I can run into just about any store and buy a 4GB thumb drive for under $15 bucks. It's usually not something to worry about. even my cell phone has a 4GB chip in it.
Never ascribe to malice that which can be adequately explained by stupidity.
subject: Why non-recursive sorting algorithms are not popular?