使用quickSort时出现stackoverflowerror,我可以增加堆栈和堆吗?
java中可以增加栈和堆吗?我用的是BlueJ。
========
编辑:
这是代码:
// ***** Quick-Sort Method *****
public static void quickSort(int[] data, int first, int n)
{
int p, n1, n2;
if(n > 1)
{
p = partition(data, first, n);
n1 = p - first;
n2 = n - n1 - 1;
quickSort(data, first, n1);
quickSort(data, p+1, n2);
}
}
// ***** PRIVATE HELPER FUNCTIONS *****
public static void quickSort(int[] data)
{
quickSort(data, 0, data.length);
}
private static int partition(int[] A, int first, int n )
{
int right = first + n - 1;
int ls = first;
int pivot = A[first];
for(int i = first+1; i <= right; i++)
{
if(A[i] <= pivot)
// Move items smaller than pivot only, to location that would be at left of pivot
{
ls++;
swap(A, i, ls);
}
}
swap(A, first, ls);
return ls;
}
private static void swap(int[] data, int pos1, int pos2)
{
int temp = data[pos1];
data[pos1] = data[pos2];
data[pos2] = temp;
}
Can I increase the stack and the heap in java? I'm using BlueJ.
========
EDIT:
Here is the code:
// ***** Quick-Sort Method *****
public static void quickSort(int[] data, int first, int n)
{
int p, n1, n2;
if(n > 1)
{
p = partition(data, first, n);
n1 = p - first;
n2 = n - n1 - 1;
quickSort(data, first, n1);
quickSort(data, p+1, n2);
}
}
// ***** PRIVATE HELPER FUNCTIONS *****
public static void quickSort(int[] data)
{
quickSort(data, 0, data.length);
}
private static int partition(int[] A, int first, int n )
{
int right = first + n - 1;
int ls = first;
int pivot = A[first];
for(int i = first+1; i <= right; i++)
{
if(A[i] <= pivot)
// Move items smaller than pivot only, to location that would be at left of pivot
{
ls++;
swap(A, i, ls);
}
}
swap(A, first, ls);
return ls;
}
private static void swap(int[] data, int pos1, int pos2)
{
int temp = data[pos1];
data[pos1] = data[pos2];
data[pos2] = temp;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于溢出而尝试增加堆栈大小,就像当你的垃圾箱已满时购买更多垃圾箱而不是把它扔到垃圾场一样。
您很可能会陷入无休止的递归。你能发布你的代码吗?
Trying to increase the stack size due to an overflow, would be like buying more rubbish bins, when your bin is full instead of taking it to the dump.
Most probably you go into an endless recursion. Could you post your code?
您可以使用以下 JVM 选项:
-Xms
初始 Java 堆大小-Xmx
最大 Java 堆大小-Xss
设置线程堆栈大小(如果需要)要在 BlueJ 中默认设置这些选项,您需要执行以下操作:
bluej.defs
文件bluej.vm.args
属性(行)bluej.vm.args = -Xmx512m
将堆大小设置为最大 512 MB。我希望这有帮助。
You can use the following JVM options:
-Xms
initial java heap size-Xmx
maximum java heap size-Xss
Set thread stack sizeIf you want to set these options by default in BlueJ, you need to do the following:
bluej.defs
filebluej.vm.args
property (line) within that filebluej.vm.args = -Xmx512m
to set the heap size to a maximum of 512 MB.I hope this helps.
stackoverflow 错误通常是由于错误的递归调用造成的。您确定您没有做任何错误,例如为递归流程指定正确的退出路径(也称为终止条件)吗?
The stackoverflow error is usually because of a bad recursive call. Are you sure you're not doing anything wrong like specifying proper exit paths (aka terminating conditions ) for your recursion flow?
对我来说,它看起来像是有问题的分区
我从 wikipedia 获取此代码
to me it looks like it's the partition that's bugged
I got this code from wikipedia
最简单的快速排序实现在最坏的情况下很容易受到 O(N) 内存使用的影响。 可以修改它以在最坏的情况下使用 O(log N),通过仅递归到较小的子数组并将剩余的递归转换为 while 循环:
The simplest implementation of Quicksort is vulnerable to O(N) memory use in the worst case. It is possible to modify it to use O(log N) in the worst case, by only recursing into the smaller subarray and by turning the remaining recursion into a while loop: