Artotim
Artotim

Reputation: 85

Merge sort algorithm in ANSI C

I'm learning ANSI C and for a class I have to implement a merge sort algorithm in it. I'm following the book's guide. But, for some reason I can't have it working.

Only two digits from my list got in the right position. And for the others, new digits are created. I have no idea why this happens, I think it is the way I create the temp arrays L and R (I'm using 1.0 / 0.0 to set the last element to +INFINITY). But I'm not sure if that is the reason.

Here is my code so far:

/* C program for Merge Sort */
#include <stdio.h>
#include <stdlib.h>


void printArray(int A[], int size) {
    int i;
    for (i = 0; i < size; i++)
        printf("%d ", A[i]);
    printf("\n");
}

void merge(int arr[], int l, int m, int r) {
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;

    int L[n1 + 1], R[n2 + 1];

    for (i = 1; i <= n1; i++) {
        L[i] = arr[l + i - 1];
    }
    for (j = 1; j <= n2; j++) {
        R[j] = arr[m + j];
    }

    L[n1 + 1] = 1.0 / 0.0;
    R[n2 + 1] = 1.0 / 0.0;
    i = 1;
    j = 1;

    for (k = l; k <= r; k++) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        } else {
            arr[k] = R[j];
            j++;
        }
    }
    
    // If I delete this the code stop "working". Why???
    while (i < k && k < i) {
            i++;
    }
}

void mergeSort(int arr[], int left, int right) {

    if (left < right) {
        int middle = left + (right - left) / 2;

        mergeSort(arr, left, middle);
        mergeSort(arr, middle + 1, right);

        merge(arr, left, middle, right);
    }
}


int main() {
    int arr[] = { 12, 11, 13, 5, 6, 7 };
    int arr_size = sizeof(arr) / sizeof(arr[0]);

    printf("Given array is \n");
    printArray(arr, arr_size);

    mergeSort(arr, 0, arr_size - 1);

    printf("\nSorted array is \n");
    printArray(arr, arr_size);
    return 0;
}

Also, for some reason beyond my humble understanding when i delete the while block, that does nothing apparently, the code stops working. Can someone give me a light on this?

Upvotes: 1

Views: 243

Answers (1)

Adrian Mole
Adrian Mole

Reputation: 51825

The problem lies in your attempt to use 'infinity'. Your data are integer values, and there is no representation of infinity in the integral types.

To set a "maximum possible value," use the pre-defined INT_MAX constant. So, replace the following lines:

    L[n1 + 1] = 1.0 / 0.0;
    R[n2 + 1] = 1.0 / 0.0;

with:

    L[n1 + 1] = INT_MAX;
    R[n2 + 1] = INT_MAX;

Also, as you access the [n1 + 1] and [n2 + 1] elements of the L and R arrays, you will need to increase the size of each of those arrays by one (arrays in C start at zero and end at 'n' - 1). Like so:

    int L[n1 + 2], R[n2 + 2]; // So we can use the `... + 1` elements.

With these changes, the code you posted works (even removing the strange while loop at the end of your merge function).


Note: Although your 1.0/0.0 does produce a floating point representation of infinity, attempting to cast/convert that value to an int produces Undefined Behaviour. See: What is the result of casting float +INF, -INF, and NAN to integer in C?

As to why that strange while loop was 'required' to make your code (not quite) work: Well, once undefined behaviour is invoked, anything can happen (including the summoning of Nasal Demons from the ether). See: Undefined, unspecified and implementation-defined behavior

Upvotes: 2

Related Questions