简体   繁体   English

我试图实现合并排序,但无法找到我的代码的确切问题。 该程序没有显示错误,但我没有得到想要的 output

[英]I have tried to implement mergesort and am unable to find the exact problem of my code. The program shows no errors but I do not get a desired output

Tried implementing mergesort, below is code.尝试实现合并排序,下面是代码。 Let me know if anything is wrong.让我知道是否有任何问题。 Is there any any error in my merge function?我的合并 function 是否有任何错误? unable to point to the exact problem in my code.无法指出我的代码中的确切问题。 Any help regarding the same would be highly appreciated.任何有关相同的帮助将不胜感激。

#include <iostream>
using namespace std;

Merge function to merge the two sorted arrays合并 function 合并两个排序好的 arrays

void merge(int arr[], int start, int mid, int end) {
    int n1 = mid - start + 1;
    int n2 = end - mid;
    
    int arr1[n1], arr2[n2];
    for (int i = 0; i < n1; i++) {
        arr1[i] = arr[start + i];
    }
    for (int j = 0; j < n2; j++) {
        arr2[j] = arr[mid + 1 + j];
    }

    int i;
    int j;
    int p;
    i = 0;
    j = 0;
    p = start;
    
    while (i < n1 && j < n2) {
        if (arr1[i] <= arr2[j]) {
            arr[p] = arr1[i];
            i++;
        } else {
            arr[p] = arr2[j];
            j++;
        }
        p++;
    }
    while (i < n1) {
        arr[p] = arr1[i];
        i++;
        p++;
    }

    while (j < n2) {
        arr[p] = arr2[j];
        i++;
        p++;
    }
}

Calling mergesort recursively递归调用归并排序

void merge_sort(int arr[], int start, int end) {
    if (start >= end) {
      return;
    }
    if (start < end) {
        int mid = start + (end - 1) / 2;
        merge_sort(arr, start, mid);
        merge_sort(arr, mid + 1, end);
        merge(arr, start, mid, end);
    }
}

Print function to print the array打印 function 打印数组

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++)
        cout << arr[i] << " ";
    cout << endl;
}

Main function主function

int main() {
    int arr[] = { 6, 5, 12, 10, 9, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    
    merge_sort(arr, 0, n - 1);
    cout << "The sorted array is: " << endl;
    printArray(arr, n);
}

There are 2 bugs is the code:代码有2个错误:

  • computing the mid point as int mid = start + (end - 1) / 2;将中点计算为int mid = start + (end - 1) / 2; is incorrect.是不正确的。 You should instead write:你应该写:

     int mid = start + (end - start) / 2;
  • in the last loop in merge you increment i instead of j .merge的最后一个循环中,您增加i而不是j Note that this loop is redundant as the remaining elements of arr2 are already at the end of arr .请注意,此循环是多余的,因为arr2的其余元素已经在arr的末尾。

You follow the classic convention in mergesort implementations where end is the index of the last element in the slice.您遵循合并排序实现中的经典约定,其中end是切片中最后一个元素的索引。 This convention requires multiple +1 / -1 adjustments, it easy to get it wrong.这个约定需要多次+1 / -1调整,很容易弄错。

In C and related languages where array indices start at 0 , it is more idiomatic to use a different convention: passing start as the index of the first element of the slice and end as the index just past the last element.在 C 和数组索引从0开始的相关语言中,使用不同的约定更为惯用:将start作为切片的第一个元素的索引,将end作为最后一个元素的索引。 This allows to specify empty splices and produces cleaner code.这允许指定空拼接并产生更清晰的代码。

Here is a modified version:这是修改后的版本:

#include <iostream>

using namespace std;

// Merge function to merge the two sorted arrays
void merge(int arr[], int start, int mid, int end) {
    int n1 = mid - start;
    int n2 = end - mid;
    int arr1[n1], arr2[n2];

    for (int i = 0; i < n1; i++) {
        arr1[i] = arr[start + i];
    }
    for (int j = 0; j < n2; j++) {
        arr2[j] = arr[mid + j];
    }

    int i = 0;
    int j = 0;
    int p = start;
    
    while (i < n1 && j < n2) {
        if (arr1[i] <= arr2[j]) {
            arr[p] = arr1[i];
            i++;
        } else {
            arr[p] = arr2[j];
            j++;
        }
        p++;
    }
    while (i < n1) {
        arr[p] = arr1[i];
        i++;
        p++;
    }
    // the remaining elements from `arr2` are already in place in `arr`
}

// Calling mergesort recursively
void merge_sort(int arr[], int start, int end) {
    if (end - start > 1) {
        int mid = start + (end - start) / 2;
        merge_sort(arr, start, mid);
        merge_sort(arr, mid, end);
        merge(arr, start, mid, end);
    }
}

// Print function to print the array
void printArray(const int arr[], int size) {
    for (int i = 0; i < size; i++)
        cout << arr[i] << " ";
    cout << endl;
}

// Main function
int main() {
    int arr[] = { 6, 5, 12, 10, 9, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    
    merge_sort(arr, 0, n);
    cout << "The sorted array is: " << endl;
    printArray(arr, n);
    return 0;
}

暂无
暂无

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

相关问题 我的代码编译正常,但在 geeksforgeeks 上提交时显示分段错误.....我无法确定问题 - my code compiles fine but shows segmentation fault on submission on geeksforgeeks.....i am unable to identify the problem 在此代码中输入 10000 时,我没有得到预期的 output。 问题是关于将十进制转换为二进制 - I am not getting the expected output while entering 10000 in this code. The problem is about converting decimal to binary 我的代码出现错误。 问题:从前序和中序遍历构造二叉树 - I am getting an error in my code. Problem:Construct Binary Tree from Preorder and Inorder Traversal 无法为问题获得所需的 output - Unable to get desired output for the problem 我对 C++ 中的 STL 相当陌生,我尝试使用向量进行堆。 没有得到想要的 output - I am fairly new to STLs in C++ and i tried making a heap using vectors. Didnt get the desired output 您好,我的代码中有语法问题。 你能帮助我吗? - Greetings, I have a syntax problem in my code. Can you help me? 嗨,我对这段代码有疑问。 奇数和偶数 - Hi, i have a problem with this code. ODD and EVEN numbers 我无法理解 output 到我的 c++ 代码 - I am unable to understand the output to my c++ code 我没有得到想要的 output 反向数字代码 C++ - I am not getting desired output in reverse a number code in C++ 我在代码的这一部分中遇到了几个错误,我试图将 output 转换为大写字母 - I am getting several errors in this section of my code in which I am trying to turn the output into capital letters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM