[英]I need help troubleshooting my code for MergeSort and Merge
So I've been working on MergeSort for an Algorithm project, but I've ran into various problems when it comes to getting the code to sort the arrays. 因此,我一直在为算法项目研究MergeSort,但是在获取代码以对数组进行排序时遇到了各种问题。 Whenever I generate a string and put it into MergeSort, it seems to just come out exactly the same. 每当我生成一个字符串并将其放入MergeSort时,它看起来就完全一样。 I want some help in finding where the mistake in my code is, why is it giving me this, and a solution with a simple, but good explanation. 我需要一些帮助,以查找我的代码中的错误在哪里,为什么给我这个错误以及一个简单但很好的解释的解决方案。
Here's what I've tried in the past: 这是我过去尝试过的方法:
arr[0]
instead of arr
, but it throws me an error with it being unable to convert from int
to int[]
. 我尝试使用return arr[0]
而不是arr
,但是由于无法将int
转换为int[]
而使我出错。 return merge(arr1, arr2)
but the system throws an error telling me I have to return something. 我试图删除return merge(arr1, arr2)
但是系统抛出一个错误,告诉我必须返回一些东西。 Merge method: 合并方法:
private static int[] merge(int[] a, int[] b)
{
int[] c = new int[a.length + b.length];
int counterA = 0;
int counterB = 0;
int counterC = 0;
while (counterA != a.length && counterB != b.length)
{
if (a[counterA] < b[counterB])
{
c[counterC] = a[counterA];
counterA++;
counterC++;
}
else
{
c[counterC] = b[counterB];
counterB++;
counterC++;
}
}
while (counterB == b.length && counterA != a.length)
{
c[counterC] = a[counterA];
counterA++;
counterC++;
}
while (counterA == a.length && counterB != b.length)
{
c[counterC] = b[counterB];
counterB++;
counterC++;
}
return c;
}
MergeSort method: MergeSort方法:
public static int[] mergeSort(int[] arr)
{
if (arr.length == 1)
{
return arr[0];
}
int[] arr1 = new int[arr.length / 2];
int[] arr2 = new int[arr.length - arr1.length];
for (int i = 0; i < arr1.length; i++)
{
arr1[i] = arr[i];
}
for (int i = 0; i < arr2.length; i++)
{
arr2[i] = arr[i + arr1.length];
}
arr1 = mergeSort(arr1);
arr2 = mergeSort(arr2);
return merge(arr1, arr2);
}
Because the array is randomly generated, an example would be this: 由于该数组是随机生成的,因此示例如下:
9, 1, 7, 5, 7, 2, 2, 9, 8, 9
The intended result should be this: 预期结果应为:
1, 2, 2, 5, 7, 7, 8, 9, 9, 9
However, this is what the output is instead (The array comes out unchanged): 但是,这是输出的结果(数组不变):
9, 1, 7, 5, 7, 2, 2, 9, 8, 9
Your code doesn't compile as written. 您的代码未按书面要求进行编译。 In mergeSort
, if the array length is equal to 1, it should return arr
. 在mergeSort
,如果数组长度等于1,则应return arr
。 After this change, your code works fine. 进行此更改后,您的代码可以正常工作。
We can, however, make a few small changes to your code to make it cleaner. 但是,我们可以对您的代码进行一些小的更改以使其更简洁。 Notice that for your last while loop in merge
, the check counterA == a.length
will always be true. 请注意,对于您在merge
最后一个while循环,检查counterA == a.length
将始终为true。 Therefore, we can remove it. 因此,我们可以将其删除。 Also, incrementing counters can be done while accessing the array. 同样,在访问数组时可以完成递增计数器。 Combining these suggestions results in the following code for your merge
method: 合并这些建议将为您的merge
方法生成以下代码:
private static int[] merge(int[] a, int[] b)
{
int[] c = new int[a.length + b.length];
int counterA = 0;
int counterB = 0;
int counterC = 0;
while(counterA != a.length && counterB != b.length)
{
if(a[counterA] < b[counterB])
c[counterC++] = a[counterA++];
else
c[counterC++] = b[counterB++];
}
while(counterB == b.length && counterA != a.length)
c[counterC++] = a[counterA++];
while(counterB != b.length)
c[counterC++] = b[counterB++];
return c;
}
The only problem I see in the code is the return arr[0];
我在代码中看到的唯一问题是return arr[0];
instead of return arr;
而不是return arr;
or return new int[]{arr[0]};
或return new int[]{arr[0]};
in mergeSort. 在mergeSort中。
Aside from that the code works as expected. 除此之外,代码按预期工作。
If you are not seeing the correct output, you probably aren't using the output correctly. 如果您没有看到正确的输出,则可能是您使用的输出不正确。
Here is an example of how I tested it. 这是我如何测试的示例。
public static void main(String[] args) {
int[] input = new int[]{9, 1, 7, 5, 7, 2, 2, 9, 8, 9};
int[] output = mergeSort(input);
}
There are 2 problems in your code: 您的代码中有2个问题:
arr[0]
instead of the array itself arr
您返回数组元素arr[0]
而不是数组本身arr
arr.length == 0
. 您不处理arr.length == 0
的情况。 It should return arr
as well. 它也应该返回arr
。 Note that the code can be simplified: 请注意,可以简化代码:
++
operator on index values when copying the values, 您可以在复制值时对索引值使用++
运算符, while
loops. 您应该在第二和第三个while
循环中删除冗余测试。 Here is an improved version: 这是一个改进的版本:
private static int[] merge(int[] a, int[] b)
{
int[] c = new int[a.length + b.length];
int counterA = 0;
int counterB = 0;
int counterC = 0;
while (counterA != a.length && counterB != b.length)
{
if (a[counterA] <= b[counterB])
c[counterC++] = a[counterA++];
else
c[counterC++] = b[counterB++];
}
while (counterA != a.length)
c[counterC++] = a[counterA++];
while (counterB != b.length)
c[counterC++] = b[counterB++];
return c;
}
public static int[] mergeSort(int[] arr)
{
if (arr.length < 2)
return arr;
int[] arr1 = new int[arr.length / 2];
int[] arr2 = new int[arr.length - arr1.length];
for (int i = 0; i < arr1.length; i++)
arr1[i] = arr[i];
for (int i = 0; i < arr2.length; i++)
arr2[i] = arr[i + arr1.length];
arr1 = mergeSort(arr1);
arr2 = mergeSort(arr2);
return merge(arr1, arr2);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.