Generic Mergesort

A few month ago I wrote several basic sort algorithm for educational purposes as generic as possible in C++. Here I want to present an Generic Mergesort:

 

What did I do here? Very simple; I want to avoid to allocate any new memory actively myself and try to write readable algorithm with as few loops as possible.

Obviously, I could not use the ‘standard’ way implementing merge not very readable. I also needed an in-place looking variant. So I started at looking what I actually do if I try to merge something in itself, what mergesort does in the end.

According to the STL documentationstd::upper_bound does the following:

Returns an iterator pointing to the first element in the range [first, last) that is greater than value, or last if no such element is found. The range [first, last) must be at least partially ordered, i.e. partitioned with respect to the expression !(value < element) or !comp(value, element). A fully-sorted range meets this criterion. The first version uses operator< to compare the elements, the second version uses the given comparison function comp.

I call this function twice, first to receive an Iterator of the first part of my array which is greater than the Item in the middle and then for my middle part which is greater than the Item behind my first Iterator of my array. Then I rotate those two “newly build” ranges.

According to the documentationstd::rotate does the following:

Performs a left rotation on a range of elements. Specifically, std::rotate swaps the elements in the range [first, last) in such a way that the element n_first becomes the first element of the new range and n_first – 1 becomes the last element. A precondition of this function is that [first, n_first) and [n_first, last) are valid ranges.

 

The precondition is always fulfilled because our ranges are within the array we want to merge stuff. This will be repeated as long first is not mid and mid is not equal to last, otherwise we merged our ranges.

What follows is simply the the algo for merge sort using the above implementation of merge:

 

Leave a Reply

Your email address will not be published. Required fields are marked *