C++ C++;导致零的递归合并排序传递指针

C++ C++;导致零的递归合并排序传递指针,c++,recursion,sorting,mergesort,C++,Recursion,Sorting,Mergesort,我在一个赋值中遇到了一些问题,在这个赋值中,我要转换一个特定的递归合并排序算法,该算法使用向量,但使用数组 到目前为止,我已经知道了,Sort()工作得很好,我相信。但是,Merge()是我认为问题所在…谢谢 #include "genlib.h" #include <iostream> /* Private function prototypes */ void Sort(int arr[], int n); void Merge(int *arr[], int *arr1[],

我在一个赋值中遇到了一些问题,在这个赋值中,我要转换一个特定的递归合并排序算法,该算法使用向量,但使用数组

到目前为止,我已经知道了,Sort()工作得很好,我相信。但是,Merge()是我认为问题所在…谢谢

#include "genlib.h"
#include <iostream>

/* Private function prototypes */
void Sort(int arr[], int n);
void Merge(int *arr[], int *arr1[], int *arr2[], int n1, int n2);

/* Main program */

int main() {
    int arr[] = {88, 10, 20, 50, 7, 44, 99, 9, 900, 44};
    int n = sizeof(arr) / sizeof(arr[0]);   

    Sort(arr, n);

    cout << "[";
    for (int i = 0; i < n; i++) {           // prints out sorted array
        if (i > 0) cout << ", ";
        cout << arr[i];
    }
    cout << "]" << endl;
    return 0;
}


/*
* Function: Sort
* Usage: void MergeSort(int arr[], const int START, const int END);
* ----------------------------------------------------------------------------------
* This function sorts the elements of the array into increasing numerical order 
* using the merge sort algorithm, which consists of the following steps:
*   1. Divide the array into two halves.
*   2. Sort each of these smaller array recursively.
*   3. Merge the two arrays back into the original one.
*
* NOTE: Although in the book, the creation of the 2 divided arrays would occur in 
*       this function, I had a lot of difficulty trying to get the recursive sort
*       to work with dynamic arrays. This is due to my inability to delete the 
*       array from memory before it gets called again.
*       I opted to dynamically create the array in Merge() instead.
*/

void Sort(int arr[], int n) {

    if (n <= 1) return; // base case

    int mid = n/2;

    int *arr1 = NULL; 
    int *arr2 = NULL; 

    arr1 = new int[mid];
    arr2 = new int[n-mid];

    for (int i=0; i<n; i++) {
        if (i < (mid)) {
            arr1[i] = arr[i];
        } else {
            arr2[i-(mid)] = arr[i];
        }
    }

    Sort(arr1,mid);
    delete[] arr1;
    Sort(arr2,n-mid);
    delete[] arr2;
    for (int i=0; i<n; i++) {
        arr[i] = 0;
    }
    Merge(&arr, &arr1, &arr2, n/2, n/2);
}

/*
* Function: Merge
* Usage: void Merge(int arr[], const int START, const int MID, const int END);
* ----------------------------------------------------------------------------------
* This function merges two sorted arrays into the original array, which should be 
* empty before this operation. Because the input arrays are sorted, the 
* implementation can always select the first unused element in one of the input 
* array vectors to fill the next position.
*/

void Merge(int *arr[], int *arr1[], int *arr2[], int n1, int n2) {
    int p1 = 0;
    int p2 = 0;

    while (p1 < n1 && p2 < n2) {
        if (arr1[p1] < arr2[p2]) {
            arr[p1+p2] = arr1[p1];
            p1++;
        } else {
            arr[p1+p2] = arr2[p2];
            p2++;
        }
    }
    while (p1 < n1) { 
        arr[p1+p2] = arr1[p1];
        p1++;
    }
    while (p2 < n2) { 
        arr[p1+p2] = arr2[p2];
        p2++;
    }
}
#包括“genlib.h”
#包括
/*私有函数原型*/
无效排序(int arr[],int n);
无效合并(int*arr[],int*arr1[],int*arr2[],int n1,int n2);
/*主程序*/
int main(){
int arr[]={88,10,20,50,7,44,99,9900,44};
int n=sizeof(arr)/sizeof(arr[0]);
排序(arr,n);
cout 0)cout问题处于“排序”状态:

排序(arr1,mid);
删除[]arr1;
排序(arr2,n-mid);
删除[]arr2;

对于(int i=0;i现在,到
Merge
的参数是指针数组,但它足以作为普通数组传递它们:

void Merge(int arr[], int arr1[], int arr2[], int n1, int n2)
它还可以将它们作为指针传递:

void Merge(int *arr, int *arr1, int *arr2, int n1, int n2)
我个人认为,从可读性的角度来看,第一种选择可能更好

在排序中,只需传递不带
的数组:

Merge(arr, arr1, arr2, n/2, n/2);

另外,您还有另一个非常严重的问题:在将
arr1
arr2
传递给
Merge
函数之前删除它们!这意味着
Merge
将访问未分配的内存,这是非常糟糕的。

为什么不插入一些日志语句以查看每次操作时发生的情况非常感谢,我保证凌晨三点以后停止工作任务!我真的很感谢你的帮助,谢谢!我没有足够的“声誉”去“投票”=(
void Merge(int arr[], int arr1[], int arr2[], int n1, int n2)
void Merge(int *arr, int *arr1, int *arr2, int n1, int n2)
Merge(arr, arr1, arr2, n/2, n/2);