These are part of my notes for the Coursera course: Algorithms, Pt I (Princeton).
Information on basic Mergesort can be found here.
##Bottom-Up Mergesort
####Concept
- we know :
- normal Mergesort recurs, halving the array until we hit sub-arrays/ranges of size
1
- it is only from here that sorting starts to happen
- normal Mergesort recurs, halving the array until we hit sub-arrays/ranges of size
- instead of recurring down from the full array, start with ranges of size
1
- merge all pairs of ranges up till the end of the array
- the merge operation will place them in order, sorting them
- double the sizes of the ranges
- repeat until we have a range the size of the full array
This can all be accomplished with for loops - meaning no need for recursion.
####Implementation
- outer for loop keeps track of sub-array size
- inner for loop iterates through array, merging arrays of given size
- because we start with pairs, all sub arrays are pre-sorted for the next merge operation as we go
####Analysis This merge-sort has approximately the same performance as basic merge-sort.
- outer loop takes time
~logN
- inner loop performs approx.
N
iterations
Merge-sort can only sort data that takes up at most half of memory (the other half is needed for the aux array).