马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <iostream>
using namespace std;
void mergeArry(int a[], int lfirst, int llast, int rfirst, int rlast){
int n = rlast - lfirst + 1;
int *tmp = new int[n];
int lindex = lfirst, rindex = rfirst;
int i=0;
while(lindex <= llast && rindex <= rlast)
tmp[i++] = (a[lindex] > a[rindex]? a[rindex++] : a[lindex++]);
while(lindex <= llast)
tmp[i++] = a[lindex++];
while(rindex <= rlast)
tmp[i++] = a[rindex++];
for(int j = 0; j < n; j++){
a[lfirst+j] = tmp[j];
}
delete []tmp;
}
void mergeSort(int a[], int first, int last){
if(first < last){
int middle = (last + first) >> 1;
mergeSort(a, first, middle);
mergeSort(a, middle + 1, last);
mergeArry(a, first, middle, middle + 1, last);//将两个数组合并
}
}
int main(){
int a[] = {13,45,87,21,987,23,13,0,5,23,546,78,456,2134,76,34,87,99,65,12};
int len = sizeof(a)/sizeof(a[0]);
cout<<len<<endl;
mergeSort(a, 0, 20);
cout<<len<<endl;
for(int i=0; i< len; i++)
cout<<a[i]<<' ';
cout<<endl;
}
为什么局部变量len会变,而且还插入到了数组a中,
我快被这个问题搞崩溃了
求助!!! |