我该如何解决这个问题的StackOverflowError? [英] How do I solve this StackOverflowError?
问题描述
在我的归并方案本节,我递归将一个名为改编排序的数组。要做到这一点,我创建了两个子阵,leftArr和rightArr,那么我补leftArr和rightArr与ARR上半年分别的改编的下半场。后来我就使用递归divde /排序leftArr和rightArr。
In this section of my MergeSort program, I am recursively dividing a unsorted array called "arr". To do this I create two subarrays, "leftArr" and "rightArr", then I fill "leftArr" and "rightArr" with the first half of "arr" and the second half of "arr" respectively. Afterwards I will use recursion to divde / sort leftArr and rightArr.
只是想澄清:中期= arr.length;
Just wanted clarify: mid = arr.length;
要初始化rightArr我做到以下几点:
To initialise the rightArr I do the following:
double halfLength = arr.length * 0.5;
if((!(halfLength < 0)) && (!(0 < halfLength))){
// if right array is an even num, length of right array is mid
rightArr = new int [mid];
} else
{
// else right arrays length is mid + 1
rightArr = new int[mid + 1];
}
当我这样做,我没有得到任何错误:
When I do this I get no errors:
if(arr.length % 2 == 0){
// if right array is an even num, length of right array is mid
rightArr = new int [mid];
} else
{
// else right arrays length is mid + 1
rightArr = new int[mid + 1];
}
但我的项目不允许我使用模运算符%和==操作符。
But my project doesn't allow me to use the modulus operator "%" and the "==" operator.
林没有得到任何语法错误。所有我在控制台窗口中看到的是:
异常线程mainjava.lang.StackOverflowError。
Im not getting any syntax error. All i see in the console window is: " Exception in thread "main" java.lang.StackOverflowError ".
完整递归方法是这样的:
The Complete recursive method looks like this:
public int[] mergeSort(int[] arr) {
if (arr.length < 2){
return arr; // if array has only one element, its already sorted
}
int mid = arr.length / 2; // find midpoint of array
int leftArr[] = new int [mid]; // create left subarray of length mid
int rightArr[]; // create right subarray
/* if(arr.length % 2 == 0){
// if right array is an even num, length of right array is mid
rightArr = new int [mid];
} else
{
// else right arrays length is mid + 1
rightArr = new int[mid + 1];
}*/
double halfLength = arr.length * 0.5;
if((!(halfLength < 0)) && (!(0 < halfLength))){
// if right array is an even num, length of right array is mid
rightArr = new int [mid];
} else
{
// else right arrays length is mid + 1
rightArr = new int[mid + 1];
}
// create a resultArr of size arr, to store the sorted array
int resultArr[] = new int [arr.length];
int i = 0;
// Copy first half of arr[] into leftArr[]
while(i < mid){
leftArr[i] = arr[i];
i = i + 1;
}
int j = mid;
int indexOfRight = 0;
// Copy second half of arr into rightArr
while(j < arr.length){
rightArr[indexOfRight] = arr[j];
indexOfRight = indexOfRight + 1;
j = j + 1;
}
// Recursively call mergeSort to sort leftArr and rightArr
leftArr = mergeSort(leftArr);
rightArr = mergeSort(rightArr);
// merge leftArr and rightArr into a resultant Array, and then return the resultArr
return resultArr = merge(leftArr, rightArr);
}
这是我如何合并:
public int[] merge(int[] a1, int[] a2) {
// TO BE COMPLETED
int lengthOfRes = a1.length + a2.length;
int resArr[] = new int [lengthOfRes]; // create resultant array of size a1 + a2
int a1Index = 0;
int a2Index = 0;
int resIndex = 0;
while((a1Index < a1.length) || (a2Index < a2.length))
{
if((a1Index < a1.length) && (a2Index < a2.length)){
// if a1's element is <= a2's element, then insert a1's elem in resArr
if(a1[a1Index] < a2[a2Index]){
resArr[resIndex] = a1[a1Index];
a1Index = a1Index + 1;
resIndex = resIndex + 1;
} else
// else, insert a2's elem in resArr
{
resArr[resIndex] = a2[a2Index];
a2Index = a2Index + 1;
resIndex = resIndex + 1;
}
}
// Here, if there are any of a1's elements left over, then insert them into resArr
else if(a1Index < a1.length){
resArr[resIndex] = a1[a1Index];
a1Index = a1Index + 1;
resIndex = resIndex + 1;
}
// Here, if there are any of a2's elements left over, then insert them into resArr
else
{
resArr[resIndex] = a2[a2Index];
a2Index = a2Index + 1;
resIndex = resIndex + 1;
}
}
return resArr; // return the resulting array
}
我怎样才能解决这个问题呢?
How can I fix this problem?
在此先感谢!
推荐答案
此算法不选任何东西。你只是打破了数组递归,但没有任何比较。
This algorithm is not sorting anything. You are only breaking the array recursively, but there isn't any comparison.
本网站有关于合并排序算法一个很好的解释: HTTP://algs4.cs.princeton。 EDU / 22mergesort /
http://algs4.cs.princeton.edu/22mergesort/Merge.java.html
This site have a good explanation about merge sorting algorithm: http://algs4.cs.princeton.edu/22mergesort/ http://algs4.cs.princeton.edu/22mergesort/Merge.java.html
这是值得研究的。
的问题是,此code
The problem is that this code
double halfLength = arr.length * 0.5;
if((!(halfLength < 0)) && (!(0 < halfLength)))
没有确定arr.length是偶数。试试这个:
do not determine if the arr.length is even. Try this:
public boolean isEven(int number) {
// return (number - (number / 2) * 2) == 0;
return (!((number - (number / 2) * 2) > 0)) && (!((number - (number / 2) * 2) < 0));
}
下面是没有分裂的另一种方法,MOD或等于操作
Here is another method without division, mod or equals operations
public boolean isEven(int number) {
number = number < 0 ? number * -1 : number;
if (number < 1) {
return true;
}
if (number > 0 && number < 2) {
return false;
}
return isEven(number - 2);
}
这篇关于我该如何解决这个问题的StackOverflowError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!