У меня возникли проблемы с заданием, в котором я должен был скрыть определенный алгоритм рекурсивной сортировки слиянием, который использует вектор, но вместо этого использует массив.
Вот что у меня есть, 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++;
}
}
Вот исходная векторная реализация:
/*
* 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++]);
}