Is it possible to get a stack overflow while using quicksort in average running time?

前端 未结 2 675
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 10:10

I know that this sounds stupid, but if we assume that we are lucky and get an average time complexity of O(NlogN) every single time for quick sort, then regardless of the size o

相关标签:
2条回答
  • 2021-01-27 10:28

    You cannot calculate this without having the number of elements you are considering for your input. Stack overflow error in this case would be a function of "number of elements" and "stack size". Out of these you are missing one.

    0 讨论(0)
  • 2021-01-27 10:33

    Programs using recursive approach are most likely to get to a stack overflow when the input is relatively huge. On the other hand, iterative approach is much performant and safe. As stated above the answer to your question depends on how huge your input is. But, to get an idea about how fast a recursive program can damage a stack comparing to an iterative program, you may take a look at this link: http://www.codeproject.com/Articles/21194/Iterative-vs-Recursive-Approaches

    Just as a general advice, it is not a good idea to use recursive in critical sections of your program.

    0 讨论(0)
提交回复
热议问题