Tonight, in an impromptu, I decided to code a merge sort. Why would I do that ? I know it’s a cool sorting algorithm and one of the first examples we are taught about Divide and Conquer, but it’s not a good reason to do that out of the blue anyway, so, as an attempt to make this sudden code-wish more productive, I decided to write a post looking this algorithm closely.

#### Basic Idea:

The principle behind Merge-Sort is rather simple, yet powerful. One have to keep dividing the object to be sorted (an array, in our case), until it’s unit fraction, meaning an array with one single element. From this principle, an unitary array is always sorted, and merging two sorted arrays is simple (more on that later). Then, by induction, we know how to sort two sorted arrays with 2 units, then with 4 and so on, an classic example of the divide and conquer technique.

Words are beautiful but how do we code it ? The code examples in this post are in C language (yeah, pointers, I know).

void merge_sort(int* array, int begin, int end) {
if (end - begin + 1 <= 1) return;
int m = (begin + end) / 2;
merge_sort(array, begin, m);
merge_sort(array, m + 1, end);
merge(array, begin, m, m + 1, end);
}

When I first looked at this code years ago, I just couldn’t believe this actually sorted an array. The real mystery behind this code is that it uses recursion, it’s the first ‘big rock’ we encounter in the path of learning computer science, as it just doesn’t make sense in the first sight. I’ll suppose that readers as this point are know of recursion and it’s no longer a problem.

The best way to understand what’s happening with the recursive calls is graphically. One of the best images that shows how this division happens is available in Wikipedia merge-sort page:

The merge function keeps breaking the array in 2 parts until it reaches it’s atomic part (1 single element). Try to simulate one vector division by hand to deepens the understanding of the function (give a preference for vectors with size ).

With the division part clear now, we have to understand the merge part and it’s complexity. The standard merging algorithm is presented below:

/*
* Merges two sorted pieces from *array:
* [sl, el] and [sr, er]
*/
void merge(int* array, int sl, int el, int sr, int er) {
int len_l = el - sl + 1;
int len_r = er - sr + 1;
int pos = 0;
// Creates a buffer to store the sorted interval temporarily
// It is not possible to sort two sorted arrays in O(n) without additional memory
int* buffer = (int*) malloc((len_l + len_r) * sizeof(int));
while (sl <= el && sr <= er) {
if (array[sl] <= array[sr]) {
buffer[pos++] = array[sl++];
} else {
buffer[pos++] = array[sr++];
}
}
while (sl <= el) {
buffer[pos++] = array[sl++];
}
while (sr <= er) {
buffer[pos++] = array[sr++];
}
sl -= len_l;
sr -= len_r;
for (pos = 0; pos < len_l; pos++) {
array[sl++] = buffer[pos];
}
for (pos = len_l; pos < len_l + len_r; pos++) {
array[sr++] = buffer[pos];
}
free(buffer);
}

As both array pieces are sorted we use a two-pointer technique by starting in the beginning of both arrays and keep comparing them, always adding the smallest one in a spare piece of memory so that we can retrieve from there later. As each single element from both arrays is visited only once, it’s easy to see the linear complexity in the function.

This is the time to ask whether it’s necessary to use this spare piece of memory. As an exercise, try to create a merge function that doesn’t use extra memory.

#### Complexity:

For those who are acquainted with computational complexity know have a feeling about the cost of this function. . This recurrence relation can be easily solved using the Master Theorem, but as the recursion in simple in this case, we can find the complexity by hand:

It’s clear that the recursion ends when , because is how deep the recursion goes:

Now we know that the largest possible value for is , hence:

#### Independence = Parallelism ?

If you read the past posts from this blog, you already know that I’ve just finished a parallel programming course that blew my mind, so obviously I’d try to add some parallelism to merge-sort. Unfortunately, merge-sort isn’t the best example to show the power of parallelism as other problems (Sieve of Eratosthenes, Conway’s Game of Life), but we are still able to see the improvements of doing more than one thing in the same time anyway.

The first thing to think before adding (simple?) parallelism to some computation is to identify independent regions in the algorithm. In our case, for each level of splitting and merging is independent, hence, we can make each of them to be run by a different thread.

I’ll present three ways to add parallelism to merge-sort, both them using the fork-join method through OpenMP. The first one uses parallel sections, enabling us to keep our code recursive, the second one will use the simplest OpenMP directive (parallel for) and the third one uses task parallelism,. that is new to the version 3.0 of OpenMP.

#### Parallel Sections:

Using parallel sections, we are able to create blocks of code were each statement (function call) surrounded by an OpenMP directive is given to a different thread. In this case, we can put both recursive calls of merge inside a parallel section and make each of the calls to be run with different threads:

void merge_sort(int* array, int b, int e) {
if (e - b + 1 <= 1) return;
int m = (b + e) / 2;
#pragma omp parallel
{
#pragma omp sections
{
#pragma omp section
merge_sort(array, b, m);
#pragma omp section
merge_sort(array, m + 1, e);
}
}
merge(array, b, m, m + 1, e);
}

#### Parallel For:

The directive *omp parallel for *is probably the first one taught when learning the OpenMP standard. The advantage of using it is that we can give a little less thinking about the parallelism (it’s often a bad idea to not think much about how we are doing parallelism). We just know that adding *#pragma omp parallel for, *each of the iterations will be given to a different thread. Of course, it’s not always that simple, and we need to take care about independence of variables (private, shared), also, the standard force us to have a well defined stop point to the iteration. More on the standard is available here.

void merge_sort(int* array, int len) {
int i, j;
/*
* `i` Represents the size of a block compared.
* It goes from 1 to the largest possible value smaller than N.
* It grows as a power of 2 (due to the bitwise operation << ).
*/
for (i = 1; i < len; i <<= 1) {
/*
* Now merging the sorted intervals [j, j + i - 1], [j + i, j + 2 * i - 1].
*/
#pragma omp parallel for
for (j = 0; j < len; j += 2 * i) {
int start_l = j;
int end_l = fmin(len - 1, j + i - 1);
int start_r = end_l + 1;
int end_r = fmin(len - 1, j + 2 * i - 1);
merge(array, start_l, end_l, start_r, end_r);
}
}
}

#### Task Parallelism:

Task parallelism was basically the last subject taught in course I’ve taken (apparently because it’s kind of recent in the OpenMP standard). It’s not my plan here to teach about task parallelism, in parts because there’s a lot of other resources available that can teach it way better than me, so I’ll jump right on how it’s used in this case.

As an advantage, we can get back to our recursive version, is easier to code. Here, we can spawn new threads asynchronously on demand and synchronize them with 2 directives *(task, taskwait).*

void merge_sort(int* array, int begin, int end) {
if (end - begin + 1 <= 1) return;
int m = (begin + end) / 2;
#pragma omp task
merge_sort(array, begin, m);
#pragma omp task
merge_sort(array, m + 1, end);
#pragma omp taskwait
merge(array, begin, m, m + 1, end);
}

#### Conclusion

As most of us have seen merge-sort sometime in life, it’s often good to see one of those fundamental algorithms a little closer, trying different implementations and approaches. It’s been a while since I don’t take an individual algorithm to pay much attention so it was fun.