Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is Bottom Up merge sort useful?

I've been reading "Algorithms, 4th Ed" by Sedgewick & Wayne. The book presents two ways of using merge sort. Using standard top down recursive merge sort OR a bottom up merge sort.

Is there any situation in which the bottom up merge sort is preferred over the top-down version?

like image 263
Nikunj Banka Avatar asked Jul 02 '13 05:07

Nikunj Banka


1 Answers

Recursive mergesort requires O(log n) space for the recursion stack, but the bottom-up version lets you do better (no recursion stack, just a few integers keeping track of your position in the input).

If you come across some language that doesn't support recursion and provides you with only limited memory for a stack (perhaps an embedded system?), the bottom-up version will be your only choice.

Here's a bottom-up version that shows what I mean.

like image 67
user541686 Avatar answered Oct 05 '22 19:10

user541686