Java 有一种工作合并排序,但不完全,有人能告诉我我做错了什么吗?
首先,我想从我的方法背后的推理开始。我的想法是,我将在一个偶数或奇数的数组中进食。该数组将被分解为一个2D数组,长度与原始数组相同,每个索引都有一个大小为1的数组。然后,我将继续合并索引。这适用于大小为2、4、8、16的长度。现在,我不得不稍微修改一下我的方法,因为3,5,6,7不起作用。现在,我的问题是,即使它们有效,但并非所有案例都有效。例如,长度为25不能正确返回。下面是我的代码:Java 有一种工作合并排序,但不完全,有人能告诉我我做错了什么吗?,java,arrays,sorting,Java,Arrays,Sorting,首先,我想从我的方法背后的推理开始。我的想法是,我将在一个偶数或奇数的数组中进食。该数组将被分解为一个2D数组,长度与原始数组相同,每个索引都有一个大小为1的数组。然后,我将继续合并索引。这适用于大小为2、4、8、16的长度。现在,我不得不稍微修改一下我的方法,因为3,5,6,7不起作用。现在,我的问题是,即使它们有效,但并非所有案例都有效。例如,长度为25不能正确返回。下面是我的代码: /** * A method to perform a merge sort * @param array
/**
* A method to perform a merge sort
* @param array The array being fed in
*/
public static int[] mergeSort(int[] array)
{
if (array.length == 1)
{
return array;
}
int size = array.length;
int[][] miniArrayList = new int[size][1];
for (int index = 0; index < array.length; index++)
{
miniArrayList[index][0] = array[index];
}
while (miniArrayList.length > 1)
{
if (miniArrayList.length % 2 == 0)
{
miniArrayList = mergeEven(miniArrayList);
}
else
{
miniArrayList[0] = mergeOdd(miniArrayList);
}
}
return miniArrayList[0];
}
/**
*执行合并排序的方法
*@param array正在传入的数组
*/
公共静态int[]合并排序(int[]数组)
{
if(array.length==1)
{
返回数组;
}
int size=array.length;
int[]miniArrayList=新的int[size][1];
for(int index=0;index1)
{
如果(miniArrayList.length%2==0)
{
miniArrayList=merge偶数(miniArrayList);
}
其他的
{
miniArrayList[0]=合并奇数(miniArrayList);
}
}
返回miniArrayList[0];
}
我对上述方法的想法是,我输入一个数组,它将其分解,然后它一点一点地合并数组,直到得到一个大小为1的排序数组。上述方法调用以下方法:
private static int[][] mergeEven(int[][] array)
{
int[][] tempSortList = new int[array.length / 2][];
int tempIndex = 0;
for (int index = 0; index < array.length; index += 2)
{
tempSortList[tempIndex] = merge(array[index], array[index + 1]);
if (tempIndex != tempSortList.length)
{
tempIndex++;
}
}
array = tempSortList;
return array;
}
private static int[] mergeOdd(int[][] array)
{
/**
* The concept is to call the even merge method on the even part
* of the list and then once I get to one array, I merge that array
* with the extra array.
*/
int[][] localArray = new int[array.length - 1][1];
int[][] extra = new int[1][1];
for (int index = 0; index < localArray.length; index++)
{
localArray[index][0] = array[index][0];
}
extra[0][0] = array[array.length - 1][0];
int[][] tempSortList = new int[localArray.length / 2][];
int tempIndex = 0;
for (int index = 0; index < localArray.length; index += 2)
{
tempSortList[tempIndex] = merge(localArray[index], localArray[index + 1]);
if (tempIndex != tempSortList.length)
{
tempIndex++;
}
}
localArray = tempSortList;
return localArray[0] = merge(localArray[0], extra[0]);
}
private static int[]]merge偶数(int[]]array)
{
int[][]tempSortList=new int[array.length/2][];
int tempIndex=0;
对于(int index=0;index
这是不言自明的,但以防万一。由于数组为奇数或偶数,上述方法排序不同。最后,这些方法调用实际的合并方法(我认为这是可行的):
/**
*将较小数组合并到较大数组的合并方法
*阵列
*@param arrayon输入的第一个数组
*@param array第二个数组正在传入
*@返回一个新的整数数组,该数组是
*两个数组的长度
*/
私有静态int[]合并(int[]arrayOne,int[]arrayTwo)
{
/*
*使用适当的方法处理奇数数组大小
*查看是否为奇数,仅合并数组的长度-1
*然后一旦全部合并,将该数组与数组的最后一部分合并
*/
//创建新子阵列的大小
int[]达雷;
if(arrayOne.length%2==0&&arrayTwo.length%2==0)
{
int size=arrayOne.length;
int doubleSize=2*size;
mergedArray=新整数[doubleSize];
//每个阵列的位置
int-posOne=0;
int-posTwo=0;
int-mergeIndex=0;
while(posOne
所以我的问题是,如果我输入的数组大小合适,它只适用于某些大小。我读了一些文章,但我的实现与我所看到的大多数不同,这就是我在这里发布的原因。我不想只是复制一个工作的,我想知道我做错了什么,这样我就可以修复它。提前感谢大家的帮助 您的问题在
merge
的逻辑中。通过使用线路
int size = arrayOne.length;
int size = arrayOne.length;