简体   繁体   English

Array 为这个归并排序函数提供了正确的输出,但 vector 给出了不正确的输出。 出了什么问题?

[英]Array gives correct output for this mergesort function but vector is giving incorrect output. What is going wrong?

I've been trying to do the count inversions question using mergesort for the past 2-3 days and after much trying, I just picked up the answer from Hackerrank's editorial, now their code is using an Array , and if I use a Vector instead of an Array , the answer is Actual answer + 1 (or different to say the least haven't tried it on many cases).在过去的 2-3 天里,我一直在尝试使用合并排序来解决计数反转问题,经过多次尝试,我刚刚从 Hackerrank 的社论中找到了答案,现在他们的代码使用的是Array ,如果我使用的是VectorArray ,答案是Actual answer + 1 (或不同,至少在很多情况下都没有尝试过)。 I was wondering what might be the reason for it.我想知道可能是什么原因。

I also have another question on explanation of this code, in particular the variable declarations and their use in the mergei function.我还有另一个关于此代码解释的问题,特别是变量声明及其在 mergei 函数中的使用。 I understand the rest of the code conceptually, but because of this part, I have some confusion.我从概念上理解代码的其余部分,但由于这一部分,我有些困惑。

    int ni = ((i+j)/2) + 1, nj = j + 1;
    int s = i;
    int* arr = new int [j - i + 1];
    j = ni; int k = 0;

Code:代码:

void mergei(int a[], int i, int j) {
    int ni = ((i+j)/2) + 1, nj = j + 1;
    int s = i;
    int* arr = new int [j - i + 1];
    j = ni; int k = 0;

    while(i < ni && j < nj) {
        if(a[i] <= a[j]) {
            arr[k++] = a[i++];
        } else {
            arr[k++] = a[j++];
            ans += (ni-i);
        }
    }

    for(; i < ni; i++, k++) arr[k] = a[i];
    for(; j < nj; j++, k++) arr[k] = a[j];
    for(k = 0; s < nj; s++, k++) a[s] = arr[k];
    delete [] arr;
}

void m_sort(int a[], int i, int j) {
    if(i < j) {
        m_sort(a, i, (i+j)/2);
        m_sort(a, ((i+j)/2) + 1, j);
        mergei(a, i, j);
    }
}

int main() {
    // vector<int> a = {2, 1, 3, 1, 2};
    int a[] = {2, 1, 3, 1, 2};
    // int n = a.size();
    int n = sizeof(a)/sizeof(a[0]);
    m_sort(a, 0, n - 1);
    cout << ans << endl;
    return 0;
}

我没有通过引用传递 Vector,在数组的情况下我不必担心。

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

相关问题 我正在执行阵列旋转但得到不正确的输出。 谁能告诉我下面的代码有什么问题? - I was performing array rotation but getting incorrect output. anyone can tell me what is wrong with the code below? 将数组分配给std :: vector会给出错误的输出 - Assigning array to std::vector gives incorrect output 给char值作为数组索引给出错误的输出 - giving char value as array index gives wrong output Mergesort错误输出C ++ - Mergesort incorrect output c++ 输出不正确。 C ++ primer 1.4.4 - Incorrect output. C++ primer 1.4.4 我对“烂橙子”问题的解决方案给出了错误的输出。 我已经使用 BFS 实现了它 - My solution of "Rotten Oranges" problem gives incorrect output. I've implemented it using BFS 输出整个向量/数组的正确方法是什么? [C ++] - What is the correct way to output a whole vector/array? [C++] 如果我采用 A = 10 ^ 9 和 B = 10 ^ 9,则以下代码给出负 output。 为什么? 对于小整数,它给了我正确的 output - if i am taking A = 10^9 and B=10^9 than following code is giving negative output. why? It is giving me correct output for small integers Boost::geometry::intersection 给出错误的输出。 版本 BOOST_1_57_0 - Boost::geometry::intersection gives wrong output. version BOOST_1_57_0 得到错误的输出。 垃圾值 - Getting wrong output. Garbage value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM