• + 1 comment

    Dynamic memory is not the correct way to handle this. The correct way is to use an iterative solution rather than the recursive solution.

    • + 0 comments

      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.