归并排序的问题
#include <iostream>
using namespace std;
void Merge(int a[], int tmp[], int lPos, int rPos, int rEnd)
{
int lEnd = rPos - 1;
int tmpPos = lPos;
while(lPos <= lEnd && rPos <= rEnd)
{
if(a[lPos] <= a[rPos])
tmp[tmpPos++] = a[lPos++];
else
tmp[tmpPos++] = a[rPos++];
}
while(lPos <= lEnd)
tmp[tmpPos++] = a[lPos++];
while(rPos <= rEnd)
tmp[tmpPos++] = a[rPos++];
int numElements = rEnd - lPos + 1;
for(int i; i < numElements; i++)
a[i] = tmp[i];
}
void Msort(int a[], int tmp[], int low, int high)
{
if(low < high)
return;
int middle = (low + high) / 2;
Msort(a, tmp, low, middle);
Msort(a, tmp, (middle + 1), high);
Merge(a, tmp, low, (middle + 1), high);
}
void MergeSort(int a[], int len)
{
int *tmp = new int[len];
if(tmp != NULL)
{
Msort(a, tmp, 0, len - 1);
delete []tmp;
}
}
int main()
{
int a[] = {1, 3, 4, 2, 8, 5, 6};
MergeSort(a, 7);
cout << "The sorted array is:" << endl;
for(int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
cout << a[i] << " ";
cout << endl;
system("pause");
return 0;
}
这道归并排序为为什么不想作用了?