Merge Sort Implementations in C and C#

suggest change

C implementation

#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>

static void merge(int values[], int scratch[], size_t first,
                  size_t middle, size_t last) {
    size_t left = first;
    size_t right = middle;
    size_t destination = first;

    while (left < middle && right < last) {
        scratch[destination++] = values[left] <= values[right]
            ? values[left++] : values[right++];
    }
    while (left < middle) scratch[destination++] = values[left++];
    while (right < last) scratch[destination++] = values[right++];
    for (size_t index = first; index < last; ++index)
        values[index] = scratch[index];
}

static void merge_sort_range(int values[], int scratch[],
                             size_t first, size_t last) {
    if (last - first < 2) return;
    const size_t middle = first + (last - first) / 2;
    merge_sort_range(values, scratch, first, middle);
    merge_sort_range(values, scratch, middle, last);
    merge(values, scratch, first, middle, last);
}

// Returns false if temporary storage could not be allocated.
bool merge_sort(int values[], size_t length) {
    if (length < 2) return true;
    int *scratch = malloc(length * sizeof *scratch);
    if (scratch == NULL) return false;
    merge_sort_range(values, scratch, 0, length);
    free(scratch);
    return true;
}

This version uses half-open ranges (first is included, last is excluded), accepts arrays of any length, and does not rely on a sentinel value that might also occur in the input.

C# implementation

public class MergeSort
    {
        static void Merge(int[] input, int l, int m, int r)
        {
            int i, j;
            var n1 = m - l + 1;
            var n2 = r - m;

            var left = new int[n1];
            var right = new int[n2];

            for (i = 0; i < n1; i++)
            {
                left[i] = input[l + i];
            }

            for (j = 0; j < n2; j++)
            {
                right[j] = input[m + j + 1];
            }

            i = 0;
            j = 0;
            var k = l;

            while (i < n1 && j < n2)
            {
                if (left[i] <= right[j])
                {
                    input[k] = left[i];
                    i++;
                }
                else
                {
                    input[k] = right[j];
                    j++;
                }
                k++;
            }

            while (i < n1)
            {
                input[k] = left[i];
                i++;
                k++;
            }

            while (j < n2)
            {
                input[k] = right[j];
                j++;
                k++;
            }
        }

        static void SortMerge(int[] input, int l, int r)
        {
            if (l < r)
            {
                int m = l + (r - l) / 2;
                SortMerge(input, l, m);
                SortMerge(input, m + 1, r);
                Merge(input, l, m, r);
            }
        }

        public static int[] Main(int[] input)
        {
            SortMerge(input, 0, input.Length - 1);
            return input;
        }
    }

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents