-2

Naive sorts like Bubble Sort and Insertion Sort are inefficient and hence we use more efficient algorithms such as Quicksort and Merge Sort. But then, these two sorts are recursive in nature, and recursion takes up much more stack memory than iteration (which is used in naive sorts) unless implemented as a tail call. How can then Quicksort and Merge Sort be more efficient than the naive sorts?

(People have identified this question as a duplicate of this question. But this is not quite the question I asked. The linked question asks about Big O notation in general whereas I am specifically concerned about the space complexity of recursive sorts. Please understand the difference.)

1 Answers1

4

What you asked is:

"How can Quicksort and Merge Sort be more efficient in terms of memory usage than the naive sorts?"

And the answer is: they aren't.

  • Complex sort algorithms like Quicksort and Mergesort are more efficient than Bubble Sort and Insertion Sort regarding to their running time, not regarding memory usage.

  • Of course, the more complex algorithms are not particular less efficient than the simple ones in memory usage , either. At least not asymptotically, since all of them require O(N) memory, where N is the number of elements to be sorted. So don't think those algorithms are generally faster because of using more memory. They are generally faster because they use more sophisticated ideas, which leads (asymptotically) to less comparisons and less movements of the sorted elements.

  • Any recursive algorithm can be converted into an iterative one. When done correctly, this is a common way to micro optimize the speed of algorithms like Quicksort and Mergesort (when done blindly, this can also slow down things). This does not change their asymptotic behaviour, however.

Doc Brown
  • 218,378