简体   繁体   English

使用数组而不是向量合并排序实现

[英]Merge sort implementation with an array used instead of vector

I am trying to implement a merge-sort algorithm with the use of arrays instead of vectors and I am getting some errors in one of my two functions. 我正在尝试使用arrays而不是vectors来实现merge-sort算法,但我的两个函数之一却出现了一些错误。 The code of the two functions is below. 这两个函数的代码如下。

void Merge(int ar[], int ar1[], int ar2[], int n1, int n2) {

    int p1 = 0;
    int p2 = 0;
    int p3 = 0;
    while (p1 < n1 && p2 < n2) {
        if (ar1[p1] < ar2[p2])
        {
            ar[p3]=ar1[p1];
            p3++;
            p1++;
        }
        else
        {
            ar[p3]=ar2[p2];
            p3++;
            p2++;
        }
    }

    while (p1 < n1)
    {
        ar[p3]=ar1[p1];
        p3++;
        p1++;
    }

    while (p2 < n2)
    {
        ar[p3]=ar2[p2];
        p3++;
        p2++;
    }
}

I figured out how to solve the problem I was facing with the code below. 我想出了如何解决下面的代码所面临的问题。

void Sortmerge(int array[],int n) {

    if (n <= 1) return;

    if (n % 2 == 0)
    {
        int arr1[n/2];
        int arr2[n/2];
        int k=0;
        int l=0;

        for (int i=0;i<n;i++)
        {
            if (i<(n/2))
            {
                arr1[k++]=array[i];
            }

            else
            {
                arr2[l++]=array[i];
            }
        }

        Sortmerge(arr1,n/2);
        Sortmerge(arr2,n/2);

        for (int i=0;i<n;i++)
        {
        array[i]=0;
        }

        Merge(array, arr1, arr2,n/2,n/2);
    }

    if (n%2!=0)
    {
        int arr1[(n-1)/2];
        int arr2[(n+1)/2];
        int k=0;
        int l=0;
        for (int i=0;i<n;i++)
        {
            if (i<((n-1)/2))
            {
                arr1[k++]=array[i];
            }

            else
            {
                arr2[l++]=array[i];
            }
        }

        Sortmerge(arr1,(n-1)/2);
        Sortmerge(arr2,(n+1)/2);

        for (int i=0;i<n;i++)
        {
            array[i]=0;
        }

        Merge(array, arr1, arr2,(n-1)/2,(n+1)/2 );
    }
}

You have declared two sets of arrays. 您已经声明了两组数组。 Two named arr1 and two named arr2 . 两个名为arr1和两个名为arr2 These have some data added to them, but then they go out of scope and the data is not used. 这些文件中添加了一些数据,但是它们超出了范围,因此不使用数据。 That is the source of the warnings. 这是警告的来源。

You then attempt to use arr1 and arr2 out of their scopes -- outside of the if blocks. 然后,您尝试在其范围之外arr2块之外使用arr1arr2 This is the source of the error. 这是错误的根源。 These arrays must be declared once, likely at the top of your function ( Sortmerge ), before your n % 2 checks. n % 2检查之前,必须在函数顶部( Sortmerge )声明一次这些数组。

When you write 当你写

if (n%2==0)
{
    int arr1[n/2]; //<-------here
    int arr2[n/2]; //<-------here
    ..........
}

arr1 and arr2 are only visible to code within that scope (between the {} s). arr1arr2仅对该范围内(在{}之间)的代码可见。 So you have two different sets of arr1 and arr2 , one for odd n and one for even n , but neither of those is visible to your Sortmerge(...) calls. 因此,您有两组不同的arr1arr2 ,一组代表奇数n ,另一组代表偶数n ,但是Sortmerge(...)调用都不可见。

Also, you can replace 另外,您可以更换

if (n%2==0){
....
}

if (n%2!=0){
....
}

with

if (n%2==0){
....
}
else{
....
}

It's seem normal. 好像很正常

You declare your two array in a if statement. 您可以在if语句中声明两个数组。 The scope for your variable is just for this statement. 变量的作用域仅用于此语句。 Replace : 更换:

   if (n%2==0)
    {
        int arr1[n/2]; //<-------here
        int arr2[n/2]; //<-------here
        int k=0;
        int l=0;

by 通过

   int arr1[n/2]; //<-------here
   int arr2[n/2]; //<-------here
   if (n%2==0)
    {
        int k=0;
        int l=0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM