Quicksort: Iterative or Recursive

后端 未结 3 727
梦毁少年i
梦毁少年i 2020-11-29 04:41

I learnt about quick sort and how it can be implemented in both Recursive and Iterative method.
In Iterative method:

  1. Push the range (0...n) into the stac
3条回答
  •  有刺的猬
    2020-11-29 05:18

    Recursion is NOT always slower than iteration. Quicksort is perfect example of it. The only way to do this in iterate way is create stack structure. So in other way do the same that the compiler do if we use recursion, and propably you will do this worse than compiler. Also there will be more jumps if you don't use recursion (to pop and push values to stack).

提交回复
热议问题