使用quickSort时出现stackoverflowerror,我可以增加堆栈和堆吗?

发布于 2024-11-08 20:53:55 字数 1107 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瑶笙 2024-11-15 20:53:55

由于溢出而尝试增加堆栈大小,就像当你的垃圾箱已满时购买更多垃圾箱而不是把它扔到垃圾场一样。

您很可能会陷入无休止的递归。你能发布你的代码吗?

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?

誰認得朕 2024-11-15 20:53:55

您可以使用以下 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 size

If you want to set these options by default in BlueJ, you need to do the following:

  • Find bluej.defs file
  • Find bluej.vm.args property (line) within that file
  • Add the option that you want in that line, i.e. bluej.vm.args = -Xmx512m to set the heap size to a maximum of 512 MB.

I hope this helps.

谎言 2024-11-15 20:53:55

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?

短叹 2024-11-15 20:53:55

对我来说,它看起来像是有问题的分区

private static int partition(int[] A, int first, int n )
{
    int right = first + n-1;
    int ls = first;
    int pivot = A[right];//use right most for pivot
    for(int i = first;i<right;i++)
    {
        if(A[i]<pivot){
           swap(A,i,ls);
           ls++;//inc after swap
        }

    }
    swap(A,right,ls);
    return ls;
}

我从 wikipedia 获取此代码

to me it looks like it's the partition that's bugged

private static int partition(int[] A, int first, int n )
{
    int right = first + n-1;
    int ls = first;
    int pivot = A[right];//use right most for pivot
    for(int i = first;i<right;i++)
    {
        if(A[i]<pivot){
           swap(A,i,ls);
           ls++;//inc after swap
        }

    }
    swap(A,right,ls);
    return ls;
}

I got this code from wikipedia

可可 2024-11-15 20:53:55

最简单的快速排序实现在最坏的情况下很容易受到 O(N) 内存使用的影响。 可以修改它以在最坏的情况下使用 O(log N),通过仅递归到较小的子数组并将剩余的递归转换为 while 循环:

//the following code probably contains of-by-one errors

quicksort(xs, begin, end):
    while(not empty list){
        mid = partition(xs, begin, end)
        if( mid-begin < end-mid){
            quicksort(xs, begin, mid)
            end = mid
        }else{
            quicksort(xs, mid, end)
            begin = mid
        }

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:

//the following code probably contains of-by-one errors

quicksort(xs, begin, end):
    while(not empty list){
        mid = partition(xs, begin, end)
        if( mid-begin < end-mid){
            quicksort(xs, begin, mid)
            end = mid
        }else{
            quicksort(xs, mid, end)
            begin = mid
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文