Skip to content

Proposal to add iterative merge sort to benchmark #19

@furkansimsekli

Description

@furkansimsekli

R4 failed to complete recursive merge sort for a given array that has 2048 integers in it. Iterative merge sort even completed its work until I switched tab to serial monitor to see the results.

At least one other algorithm also failed with 2048 int test: radix sort. We should find a way to solve this "scientifically". I mean some algorithms work but others dont, what are we gonna do? stop? I don't think so. I haven't tested with 4096 integers but I can sense that some of the algorithms will keep working even with 4096 integers. What I mean by scientific is that we need to find a way to label the failing algorithms. In this case we also need to adapt our visualization tool.

// 11. Iterative Merge Sort

void merge(int arr[], int left, int mid, int right) {
    int n1 = mid - left + 1;
    int n2 = right - mid;
    int L[n1], R[n2];

    for (int i = 0; i < n1; i++) L[i] = arr[left + i];
    for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];

    int i = 0, j = 0, k = left;
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) arr[k++] = L[i++];
        else arr[k++] = R[j++];
    }
    while (i < n1) arr[k++] = L[i++];
    while (j < n2) arr[k++] = R[j++];
}

void iterativeMergeSort(int arr[], int n) {
    for (int currSize = 1; currSize < n; currSize *= 2) {
        for (int leftStart = 0; leftStart < n - 1; leftStart += 2 * currSize) {
            int mid = leftStart + currSize - 1;
            int rightEnd = (leftStart + 2 * currSize - 1 < n - 1) ? (leftStart + 2 * currSize - 1) : (n - 1);
            if (mid < rightEnd) merge(arr, leftStart, mid, rightEnd);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions