We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Sedgewick's "Algorithms" has a couple good examples of "removing" recursion from recursive algorithms such as QuickSort, by replacing the recursion with iteration. In reality, the algorithm is still recursive, and there is still as stack, but you allocate the sorting stack on the heap, rather than using the runtime stack.
Therefore the solution dosent become iterative , it just looks iterative
so actually we just need to implement most of stack on the heap for efficient coding .
Another way to look at it, is if you think you need a big stack, your code is inefficient. There is a better way that uses less stack.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Common Child
You are viewing a single comment's thread. Return to all comments →
Dynamic memory is not the correct way to handle this. The correct way is to use an iterative solution rather than the recursive solution.
Sedgewick's "Algorithms" has a couple good examples of "removing" recursion from recursive algorithms such as QuickSort, by replacing the recursion with iteration. In reality, the algorithm is still recursive, and there is still as stack, but you allocate the sorting stack on the heap, rather than using the runtime stack.
Therefore the solution dosent become iterative , it just looks iterative so actually we just need to implement most of stack on the heap for efficient coding . Another way to look at it, is if you think you need a big stack, your code is inefficient. There is a better way that uses less stack.