自底向上归并排序
我有以下用于自下而上合并排序的代码,它对文件进行操作,每一次合并都会将 m 加倍,这是代码,
#include <iostream>
#include <vector>
using namespace std;
inline int Min(int a,int b)
{
return a<b?a:b;
}
void merge(int a[],int l,int m,int r)
{
vector<int>b;
int i, j;
for (i=m+1;i>=l;i--) b[i-1]=a[i-1];
for (j=m;j<r;j++) b[r+m-j]=a[j+1];
for (int k=l;k<=r;k++)
if ( b[j]<b[i])
a[k]=b[j--];
else
a[k]=b[i++];
}
void mergesort(int a[],int l,int r)
{
for (int m=1;m<=r-l;m=m+m)
for (int i=l;i<=r-m;i+=m+m)
merge(a,i,i+m-1,Min(i+m+m-1,r));
}
int main()
{
int a[]={12,4,7,3,9,8,10,11,6};
int n=sizeof(a)/sizeof(int);
mergesort(a,0,n-1);
for (int i=0;i<n;i++)
{
cout<<a[i]<< " ";
}
return 0;
}
但是当我运行此代码时,出现异常,表明发生了向量超出范围错误,请帮助
i have following code for bottom up mergesort it does it's operation on file m-by-m merges doubles m on each pass here is code
#include <iostream>
#include <vector>
using namespace std;
inline int Min(int a,int b)
{
return a<b?a:b;
}
void merge(int a[],int l,int m,int r)
{
vector<int>b;
int i, j;
for (i=m+1;i>=l;i--) b[i-1]=a[i-1];
for (j=m;j<r;j++) b[r+m-j]=a[j+1];
for (int k=l;k<=r;k++)
if ( b[j]<b[i])
a[k]=b[j--];
else
a[k]=b[i++];
}
void mergesort(int a[],int l,int r)
{
for (int m=1;m<=r-l;m=m+m)
for (int i=l;i<=r-m;i+=m+m)
merge(a,i,i+m-1,Min(i+m+m-1,r));
}
int main()
{
int a[]={12,4,7,3,9,8,10,11,6};
int n=sizeof(a)/sizeof(int);
mergesort(a,0,n-1);
for (int i=0;i<n;i++)
{
cout<<a[i]<< " ";
}
return 0;
}
but when i run this code there is exception which says that vector's out of range error was occured please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您尚未初始化矢量以使其包含任何数据。
我想这是一个练习,这就是为什么你要重新发明轮子。我不确定这是使用单字符标识符的借口,这会使您的代码难以理解。
如果 a 是一个数组,l 是它的长度,您可以初始化 b ,
大概您正在创建数组的临时克隆以进行排序。
顺便问一下,合并排序不是递归的吗?我没看到你的情况。
我的代码也有其他问题,例如,您的缩进表明 for 循环是嵌套的,但与 for 语句位于同一行的语句后面的分号表明不然。我建议你总是在循环上使用大括号。
You have not initialised your vector to have any data in it.
I guess this is an exercise which is why you are reinventing the wheel. I am not sure that is an excuse for using single-character identifiers which makes your code hard to understand.
If a is an array and l is its length you can initialise b with
Presumably you are creating a temporary clone of your array for the purpose of the sort.
Isn't mergesort recursive, by the way? I don't see yours being.
I have other issues with your code too, eg your indentation suggests that the for loops are nested but the semi-colons after the statements that are on the same line as the for statements suggest otherwise. I'd suggest you always use braces on your loops.
在函数
merge
中,您有vectorb;
b 的大小为 0。您应该rezise()
您的向量,或使用数组初始化它:In function
merge
you havevector<int>b;
b is of size 0 here. You shouldrezise()
your vector, or initialize it with the array:您将
b
创建为空向量,然后开始寻址其元素。它的大小为 0,因此无效。你应该给它一个更大的尺寸。You create
b
as an empty vector, and then start addressing its elements. It has size 0, so that's invalid. You should give it a larger size.其他人已经解决了尝试在空向量中索引元素的问题。此外,以下循环存在问题:
循环的最后一次迭代有
i=l
并且您寻址向量/数组的[i-1]
元素。当l=0
时,这是索引-1
并且对于向量和数组来说都超出范围。Others have addressed your problem with trying to index elements in an empty vector. In addition, the following loop has a problem:
The last iteration through the loop has
i=l
and you address the[i-1]
element of the vector/array. Whenl=0
this is the index-1
and will be out-of-range for both the vector and array.