自底向上归并排序

发布于 2024-10-04 08:10:29 字数 901 浏览 0 评论 0原文

我有以下用于自下而上合并排序的代码,它对文件进行操作,每一次合并都会将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江挽川 2024-10-11 08:10:29

您尚未初始化矢量以使其包含任何数据。

我想这是一个练习,这就是为什么你要重新发明轮子。我不确定这是使用单字符标识符的借口,这会使您的代码难以理解。

如果 a 是一个数组,l 是它的长度,您可以初始化 b ,

vector<int> b( a, a+l );

大概您正在创建数组的临时克隆以进行排序。

顺便问一下,合并排序不是递归的吗?我没看到你的情况。

我的代码也有其他问题,例如,您的缩进表明 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

vector<int> b( a, a+l );

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.

初雪 2024-10-11 08:10:29

在函数 merge 中,您有 vectorb; b 的大小为 0。您应该rezise()您的向量,或使用数组初始化它:

vector<int> v(arr, arr+size);

In function merge you have vector<int>b; b is of size 0 here. You should rezise() your vector, or initialize it with the array:

vector<int> v(arr, arr+size);
°如果伤别离去 2024-10-11 08:10:29

您将 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.

楠木可依 2024-10-11 08:10:29

其他人已经解决了尝试在空向量中索引元素的问题。此外,以下循环存在问题:

for (i=m+1;i>=l;i--)  b[i-1]=a[i-1];

循环的最后一次迭代有 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:

for (i=m+1;i>=l;i--)  b[i-1]=a[i-1];

The last iteration through the loop has i=l and you address the [i-1] element of the vector/array. When l=0 this is the index -1 and will be out-of-range for both the vector and array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文