编译没问题呀,但是提示我越界了,找了一个下午,后无意发现原来l和1弄混了……千万吸取教训!
#include<iostream>
#include<algorithm>
using namespace std;
void Merge(int *initList, int *mergedList, const int l, const int m, const int n )
//n代表有效的数字个数(除了数组首数字0外的数字个数)也是后一个有效数字的下标
//l代表第一个有效数字的下标,m代表第一组有效数字后一个数字的下标
{
int i1, i2, iResult;
for (i1 = l, i2 = m + l, iResult = l; i1 <= m && i2 <= n; iResult++)
{
if (initList[i1] <= initList[i2])
mergedList[iResult] = initList[i1++];
else
mergedList[iResult] = initList[i2++];
}
copy(initList + i1, initList + m + 1, mergedList + iResult);
copy(initList + i2, initList + n + 1, mergedList + iResult);
}
void MergePass(int *initList,int *resultList,const int n,const int s)
{
int i;
for (i = 1; i <= (n - 2 * s + 1); i += 2 * s)
Merge(initList, resultList, i, i+s-1 , i+2*s-1);
if ((i + s - 1) < n)
Merge(initList, resultList, i, i + s - 1, n);
else
copy(initList + i, initList + n + 1, resultList + i);
}
int main()
{
int m[] = { 0,26,5,77,1, 61,11,59,15,48,19 };//a[0]不用
int n[11] = {0};
MergePass(m, n, 10, 1);
for (int i = 1; i < 11;i++)
{
cout << n[i] << " ";
}
cout << endl;
MergePass(n, m, 10, 2);
for (int i = 1; i < 11; i++)
{
cout << m[i] << " ";
}
cout << endl;
MergePass(m, n, 10, 4);
for (int i = 1; i < 11; i++)
{
cout << n[i] << " ";
}
cout << endl;
MergePass(n, m, 10, 8);
for (int i = 1; i < 11; i++)
{
cout << m[i] << " ";
}
cout << endl;
return 0;
}