我正在编写一个程序,它使用两个动态数组来对原始数组进行排序:一个用于左侧,一个用于右侧。
但是,动态数组不会在第23和28行接收原始数组(如整个数组中的cout方法所示)。它们要么是空的,要么包含超出范围的元素。因此,该计划不能作为一个整体。那么我的问题是初始化本身的问题,还是第18-19行的声明?我个人认为这与宣言有关,但我不确定我应该怎么做,就像动态阵列一样,我不想太大了。我包含了所有正确测试的方法,但如果认为不必要,我会编辑这个问题。提前感谢您的帮助。
#include "stdafx.h"
#include <iostream>
using namespace std;
void Merge(int *array, int left, int middle, int right)
{
int * LArray;
int * RArray;
int counter = left;//This counter is used as a marker for the main array.
int mid = middle;
cout<<"Left " << left << "middle: "<< middle << " right: " << right<<endl;
LArray = new int[middle-left + 1];
RArray = new int[right];
/*Initializes LArray*/
for (int i = left; i < middle - left + 1; i++)
{
LArray[i] = array[i];
}
/*Initializes RArray*/
int temp = 0;
for (int i = middle; i < right; i++)
{
RArray[temp] = array[i];
temp++;
}
/*Prints out LArray*/
cout<<"LARRAY: ";
for (int i = left; i < middle- left + 1; i++)
{
cout<<LArray[i]<< " ";
}
/*Prints out RArray*/
cout<<endl<<"RARRY: ";
temp = 0;
for (int i = middle; i < right; i++)
{
temp = 0;
cout<<RArray[temp]<< " ";
temp++;
}
cout<<endl;
while (left <= middle && mid <= right)
{
/*This if statement checks if the number in the left array is smaller than the number in the right array*/
if (LArray[left] < RArray[right])
{
array[counter] = LArray[left];
left++;
counter++;
cout<<"First if: array[counter]: "<< array[counter]<<" LArray[left]" << LArray[left]<<" left: "<< left<<" counter : "<< counter<<endl;
}
/*This else statement checks if the number in the right array is smaller than the number in the left array*/
else
{
array[counter] = RArray[right];
mid++;
counter++;
cout<<" First else: array[counter]: "<< array[counter] << " RArray[right] "<< RArray[right]<<" mid: "<< mid<<" counter : "<< counter<<endl;
}
}
/*If RArray is completed, check this one for any remaining elements.*/
while (left <= middle)
{
array[counter] = LArray[left];
left++;
counter++;
cout<<" First while: array[counter]: "<< array[counter]<<" LArray[left]" << LArray[left]<<" left: "<< left<<" counter : "<< counter<<endl;
}
/*If LArray is completed, check this one for any remaining elements.*/
while (mid <= right)
{
array[counter] = RArray[right];
mid++;
counter++;
cout<<" Second while: array[counter]: "<< array[counter] << " RArray[right] "<< RArray[right]<<" mid: "<< mid<<" counter : "<< counter<<endl;
}
delete [] LArray;
delete [] RArray;
}
void MergeSort( int *array,int left, int right )
{
if ( left < right )
{
int middle = ( left + right ) / 2;
MergeSort( array, left, middle );
MergeSort( array, middle + 1, right );
Merge( array, left, middle, right );
}
};
/*Checks if the array listed is sorted by looping through and checking if the current number is smaller than the previous.*/
bool IsSorted(int* array, unsigned long long size)
{
for (int i = 0; i < size; i++)
{
cout<<array[i]<< " ";
}
cout<<endl;
for (int i = 1; i < size; i++)
{
if (array[i] < array[i-1])
return false;
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
int array[8] = {5, 2, 4, 7, 1, 3, 2, 6};
MergeSort(array, 0, 8);
bool check = IsSorted(array, 8);
if (check)
cout<<"It is sorted!";
else
cout<<"It is not sorted!";
return 0;
}
答案 0 :(得分:1)
合并排序的想法是你递归地将数组拆分成两半,对这两半进行排序,然后将它们合并在一起。
以下是我看到您的代码出错的事情:
与您的算法无关的代码怪异:
您的左侧数组总是大于必要的数组,因为您将其分配为中间元素并将其初始化为此。
您初始化右侧数组两次。
算法的实际问题:
你没有正确初始化计数器,它总是设置为0所以无论你在callstack中有多深,你总是将元素0到中间设置为排序数组而不是左右之间的范围。
您正尝试使用参数右 ...索引到正确的数组...这将始终大于右数组中的元素数量,因为您已将其分配为正确 - 中间元素。
您的MergeSort功能也有错误。你永远不会对中间元素进行排序,因为在合并函数中数组[右] 永远不会添加到 RArray ,所以第一次递归调用赢了排序它,你将中间+ 1 传递给第二个递归调用,所以它不会对它进行排序。