Arraycopy 使我的程序崩溃
我正在做一项家庭作业,我必须根据我们书籍的作者伪代码实现合并排序。 (《算法基础》,第四版,Neapolitan 和 Naimipour)。
在 main 方法中,我调用 mergeSort,其中 int n 是数组的长度,S[] 是需要排序的数组。 S[] 已经填充了 1 到 999 之间的随机数。我的程序在合并的最后一个 arraycopy 时崩溃了,我不确定为什么会这样,我已经尝试在 netbeans 中进行调试。我觉得这可能是一个我看不到的愚蠢错误。任何帮助都会很棒。提前致谢。
public static void mergesort (int n, int S[])
{
if(n>1){
final int h=n/2, m=n-h;
int U[] = new int[h];
int V[] = new int[m];
System.arraycopy(S, 0, U, 0, h);
System.arraycopy(S, h, V, 0, m);
mergesort(h, U);
mergesort(m, V);
merge(h, m, U, V, S);
}
}
public static void merge(int h, int m, final int U[], final int V[], final int S[])
{
int i=0, j=0, k=0;
while(i<h && j<m){
if(U[i]<V[j]) {
S[k] = U[i];
i++;
}
else {
S[k]=V[j];
j++;
}
k++;
}
if(i>h)
System.arraycopy(V, j, S, k, h+m);
else
System.arraycopy(U, i, S, k, h+m);
}
编辑:我意识到我在合并时犯了一些小错误。最大的变化是改变比较以使用 equals 以及理解 arraycopy 中的长度是我想要复制的元素的数量。这是我的合并方法。合并排序保持不变。
public static void merge(int h, int m, final int U[], final int V[], int S[])
{
int i=0, j=0, k=0;
while(i<h && j<m){
if(U[i]<V[j]) {
S[k] = U[i];
i++;
}
else {
S[k]=V[j];
j++;
}
k++;
}
if(i>=h)
System.arraycopy(V, j, S, k, m-j);
else
System.arraycopy(U, i, S, k, h-i);
}
感谢您的帮助。
I am working on a homework assignment where I have to implement a MergeSort based on the author of our books psuedo-code. (Foundations of Algorithms, 4th ed by Neapolitan and Naimipour).
From the main method I am calling mergeSort with the int n being the length of the array and S[] being the array needing to be sorted. S[] is already filled with random numbers between 1 and 999. My program is crashing at the last arraycopy in merge and I am not sure why it is and I have tried debugging in netbeans. I feel like this is probably a dumb mistake that I just can't see. Any help would be great. Thanks in advanced.
public static void mergesort (int n, int S[])
{
if(n>1){
final int h=n/2, m=n-h;
int U[] = new int[h];
int V[] = new int[m];
System.arraycopy(S, 0, U, 0, h);
System.arraycopy(S, h, V, 0, m);
mergesort(h, U);
mergesort(m, V);
merge(h, m, U, V, S);
}
}
public static void merge(int h, int m, final int U[], final int V[], final int S[])
{
int i=0, j=0, k=0;
while(i<h && j<m){
if(U[i]<V[j]) {
S[k] = U[i];
i++;
}
else {
S[k]=V[j];
j++;
}
k++;
}
if(i>h)
System.arraycopy(V, j, S, k, h+m);
else
System.arraycopy(U, i, S, k, h+m);
}
EDIT: I realized I had a couple small mistakes in merge. The biggest changes were changing comparisons to work with equals and also understanding the length in arraycopy is the number of elements I want to copy. Here is my merge method. mergesort stayed the same.
public static void merge(int h, int m, final int U[], final int V[], int S[])
{
int i=0, j=0, k=0;
while(i<h && j<m){
if(U[i]<V[j]) {
S[k] = U[i];
i++;
}
else {
S[k]=V[j];
j++;
}
k++;
}
if(i>=h)
System.arraycopy(V, j, S, k, m-j);
else
System.arraycopy(U, i, S, k, h-i);
}
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
测试了一下。它作为 IndexOutOfBounds。
原因是您在 ArrayCopy 中给出的长度大于源数组的计数。
Tested it. It as an IndexOutOfBounds.
The reason is that the length that you give in the ArrayCopy is greater than the count off your source-Array.