views:

111

answers:

1

I wrote the entire thing, but i get vague errors. I don't know what's wrong....ugh.Also, you may ask why non-recursive well my teacher has it that way for the test.

void nonrec_mergesort(vector <int> & a, vector <int> & b, int s, int r)
{

    int m = 1;
    while (m <= r)
    {
        int i = 0;
        while(s < (r-m))
        {
            stl_merge(a, b, i, ((i+i+m-1)/2), (i+m-1));
            stl_merge(a, b, i+m, (min(i+2*m-1,r-1)+(i+m))/2, min(i+2*m-1,r-1));
            s = s + (2*m);
        }
        m = m * 2;
    }
}
+2  A: 

This link should answer your quetsions about bottom up merge sort, but you have not provided enough information to easily assist you.

http://www.algorithmist.com/index.php/Merge_sort

Input: array a[] indexed from 0 to n-1.

    m = 1
    while m <= n do
        i = 0
        while i < n-m do
            merge subarrays a[i..i+m-1] and a[i+m .. min(i+2*m-1,n-1)] in-place.
            i = i + 2 * m
        m = m * 2
MykC