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;
}
}
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.