gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:20:01 25 4
gpt4 key购买 nike

我在分配时遇到了一些问题,我要隐藏一个特定的递归合并排序算法,该算法使用 vector ,但使用数组代替。

这是我目前所拥有的,我相信 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++;
}
}

这是原始 vector 实现:

/*
* Function: Sort
* -------------- * This function sorts the elements of the vector into
* increasing numerical order using the merge sort algorithm,
* which consists of the following steps:
*
* 1. Divide the vector into two halves.
* 2. Sort each of these smaller vectors recursively.
* 3. Merge the two vectors back into the original one.
*/
void Sort(Vector<int> & vec) {
int n = vec.size();
if (n <= 1) return;
Vector<int> v1;
Vector<int> v2;
for (int i = 0; i < n; i++) {
if (i < n / 2) {
v1.add(vec[i]);
} else {
v2.add(vec[i]);
}
}
Sort(v1);
Sort(v2);
vec.clear();
Merge(vec, v1, v2);
}

/*
* Function: Merge
* --------------- * This function merges two sorted vectors (v1 and v2) into the
* vector vec, which should be empty before this operation.
* Because the input vectors are sorted, the implementation can
* always select the first unused element in one of the input
* vectors to fill the next position.
*/
void Merge(Vector<int> & vec, Vector<int> & v1, Vector<int> & v2) {
int n1 = v1.size();
int n2 = v2.size();
int p1 = 0;
int p2 = 0;
while (p1 < n1 && p2 < n2) {
if (v1[p1] < v2[p2]) {
vec.add(v1[p1++]);
} else {
vec.add(v2[p2++]);
}
}
while (p1 < n1) vec.add(v1[p1++]);
while (p2 < n2) vec.add(v2[p2++]);
}

最佳答案

问题出在“排序”:

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);

首先对数组进行排序,然后删除它(结果 arr1 是一个空指针)你对 arr2 做同样的事情。

当你开始合并的时候,你已经删除了数据,释放了内存

一个解决方案可能是,将“删除”语句移动到合并调用下方:

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

这样您就不会丢失数据。因为您使用“new”- 语句创建数组,所以您不应该试图离开“delete”。

希望这能解决你的问题

关于C++递归合并排序传递指针导致零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8382997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com