Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有人知道为什么我的快速排序算法在大型数据集(例如长度为100000的数组)上出现堆栈溢出错误吗?_Java_Arrays_Sorting_Recursion_Quicksort - Fatal编程技术网

Java 有人知道为什么我的快速排序算法在大型数据集(例如长度为100000的数组)上出现堆栈溢出错误吗?

Java 有人知道为什么我的快速排序算法在大型数据集(例如长度为100000的数组)上出现堆栈溢出错误吗?,java,arrays,sorting,recursion,quicksort,Java,Arrays,Sorting,Recursion,Quicksort,我的Java快速排序算法在长数组(例如长度为100000的数组)上抛出堆栈溢出错误。我用三个中位数的方法找到我的轴心点。我附上了我的中位数(中位数)和快速排序方法(qSortB)。有人知道为什么会发生这种错误吗?谢谢你的帮助 //Finding the pivot using the median of three method public int median(int low, int high){ int p; int temp; int min= list[low

我的Java快速排序算法在长数组(例如长度为100000的数组)上抛出堆栈溢出错误。我用三个中位数的方法找到我的轴心点。我附上了我的中位数(中位数)和快速排序方法(qSortB)。有人知道为什么会发生这种错误吗?谢谢你的帮助

//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);
                }
            }   



        }
    }
//使用三位数法查找轴心点
公共整数中值(整数低,整数高){
INTP;
内部温度;
int min=列表[低];
//System.out.println(“min:+min”);
int med=列表[(高+低)/2];
//System.out.println(“med:+med”);
int max=列表[高];
//System.out.println(“max:+max”);
如果((最小值>=中等值)!=(最小值>=最大值)){
p=最小值;
}
否则,如果((平均值>=最小值)!=(平均值>=最大值)){
p=med;
温度=列表[(高+低)/2];
列表[(高+低)/2]=列表[低];
列表[低]=温度;
}
否则{
p=最大值;
温度=列表[高];
列表[高]=列表[低];
列表[低]=温度;
}
返回p;
}
公共无效qSortB(整数低,整数高){

如果(low>=high | | high在传入大数组时出现堆栈溢出错误,因为您必须记住自己在函数中的位置。在
qSortB()
中,您调用的是
qSortB()
再次,这意味着您调用了另一个函数,但没有结束上一个函数,因为您必须记住自己的位置,这会占用更多内存,直到导致堆栈溢出为止

要解决这个问题,您不需要使用如此大的数组,也不需要重新编写函数以使用循环而不是调用自身,这样函数将结束,从而防止堆栈溢出错误


正如Fantagirocco所提到的,我建议在进行函数调用(递归或非递归)时使用一点堆栈空间。我想,对
qSortB()的递归调用太多了
。这回答了你的问题吗?代码正在试图修改霍尔分区方案以将轴心放在适当的位置,这不是霍尔分区方案通常的工作方式。请看一下。3的中位数应该只是比较和交换,伪代码:
| md=lo+(hi-lo)/2 | if(a[lo]>a[hi])交换(a[lo],a[hi交换(a[hi交换)| if(a[lo]>a[md交换(a[lo],a[md]|如果(a[md]>a[hi])交换(a[md],a[hi])|
。使用wiki方案,轴不能位于a[hi],并且此注释中所示的中位数3不会导致这种情况发生。