简体   繁体   English

有谁知道为什么我的快速排序算法在大数据集上出现堆栈溢出错误,例如。 长度为 100,000 的数组?

[英]Does anyone know why my quick sort algorithm is getting a stack overflow error on large data sets ex. an array of length 100,000?

My Java quick sort algorithm is throwing a stack overflow error on arrays that are long such as an array of length 100,000.我的 Java 快速排序算法在长数组(例如长度为 100,000 的数组)上引发堆栈溢出错误。 I am finding my pivot using the median of three method.我正在使用三种方法的中位数找到我的支点。 I have my median(median) and quick sort method(qSortB) enclosed.我有我的中位数(中位数)和快速排序方法(qSortB)。 Does anyone know why this error is occurring?有谁知道为什么会发生这个错误? Thank you for you help.谢谢你的帮助。

//Finding the pivot using the median of three method
public int median(int low, int high){
    int p;
    int temp;
    int min= list[low];
    //System.out.println("min: "+ min);
    int med=list[(high+low)/2];
    //System.out.println("med: "+ med);
    int max= list[high];
    //System.out.println("max: "+ max);
    if ((min >= med) != (min >= max)) {
      p= min;
    }
  else if ((med >= min) != (med >= max)) {
      p= med;
        temp=list[(high+low)/2];
        list[(high+low)/2] = list[low];
        list[low] =temp;
  }
  else {
      p= max;
      temp=list[high];
        list[high] = list[low];
        list[low] =temp;

  }


    return p;

}

 public void qSortB(int low, int high){
        if(low>=high|| high<=low){
            return;
        }
        else{
            int left=low+1;
            int right=high;
            int pivot =median(low,high);
            //System.out.println("Pivot: "+ pivot);
            int pi=low;
            while(left<=right){
                while(left <len && list[left]< pivot){
                    comp++;
                    left++;
                }
                while(right >-1 && list[right] >pivot){
                    comp++;
                    right--;
                }
                if(left <len && right>-1 && left<=right){
                    comp++;
    //              System.out.println("Swapping "+list[right]
    //                      +" with " + list[left]);

                    int temp=list[left];
                    list[left] = list[right];
                    list[right] = temp;
                    left++;
                    right--;
                    swap++;
                    //print();

                }
                if(left>right){
                    int temp= list[left-1];
                    list[left-1]= pivot;
                    list[pi]= temp;
                    pi=left-1;
                    swap++;
                    qSortB(low,pi-1);
                    qSortB(pi+1,high);
                }
            }   



        }
    }

You have a stack overflow error, when you pass in a large array since you must remember where you are inside the function.当您传入一个大数组时,您会遇到堆栈溢出错误,因为您必须记住您在函数内的位置。 In qSortB() you are calling qSortB() again, which means you call another function, while not ending your previous one because you must remember where you are, this takes up more memory until it causes the stack to overflow.qSortB()您再次调用qSortB() ,这意味着您调用了另一个函数,而不是结束前一个函数,因为您必须记住自己的位置,这会占用更多内存,直到导致堆栈溢出。

To fix this, you need to not use such large arrays, or rework the function to use a loop instead of calling itself, that way the function will end, preventing the stack overflow error.要解决这个问题,您不需要使用如此大的数组,或者重新编写函数以使用循环而不是调用自身,这样函数就会结束,防止堆栈溢出错误。

As fantaghirocco mentioned, I recommend looking at this link正如 fantaghirocco 提到的,我建议查看此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算大型数组的平均中位数(最多100,000个元素) - Calculating the average median of a large array (up to 100,000 elements) 在我的合并排序中获取堆栈溢出错误 - Getting a stack overflow error in my Merge Sort 使用Shamir算法加密数字数据集(约100,000) - Use Shamir algorithm to encrypt data set of numbers (approx. 100,000) 为100,000个整数的数组计算反转,为什么要得到负输出? - Counting Inversions for an Array of 100,000 Integers, Why Get a Negative Output? 合并排序算法中的堆栈溢出错误? - Stack Overflow error in Merge Sort algorithm? 为什么这种快速排序会导致几乎排序的列表和排序列表上的堆栈溢出? - Why does this quick sort cause stack overflow on nearly sorted lists and sorted lists? 我的快速排序方法有一个基本案例,但我仍然收到堆栈溢出错误? - I have a base case for my quick sort method but I still get a stack overflow error? 编码堆排序算法,但出现堆栈溢出错误,无法弄清原因 - Coding Heapsort Algorithm but am getting a Stack Overflow error and cannot figure out why 为什么我的合并排序不适用于数组长度10的原因 - is there a reason why my merge sort does not work for array length 10 以字符串形式打印大量数字/循环运行10次幂100,000 - Printing of large numbers as Strings/ Running a loop for 10 power 100,000
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM