Quicksort Stack溢出错误

时间:2015-04-01 14:23:55

标签: java recursion stack-overflow quicksort

在练习考试时,我遇到了一个(对我来说)快速排序的奇怪问题。

我的实施:

    public void quicksort(int l, int r)
{
    if(l<r && l>0 && r<=array.length-1)
    {
        int pivot = array[pivot(l, r)];
        int i = l;
        int j = r;
        if(j==i+1)
        {
            if(array[i]>array[j])
            {
                System.out.println(array[i]+"<->"+array[j]);
                int help = array[i];      
                array[i] = array[j];  
                array[j] = help;    
            }
        }
        else{ while(i<=j)
            {
                if(array[i]>=pivot && pivot>= array[j])
                {
                    System.out.println(array[i]+">="+pivot+">="+array[j]);

                    int help = array[i];      
                    array[i] = array[j];  
                    array[j] = help;
                    i++;
                    j--;
                }
                else{
                    i++;
                }
            }
            if(l<j && j<array.length-1)quicksort(l, j);
            if(i<r)quicksort(i, r);
        }
    }
}

但是这给了我在(这里)第34行中的“Java.lang.StackOverflowError:null”。但是,可以通过在第34行和第35行中将j与j-1和i交换为j来避免此错误。我真的尝试了所有想到的东西,但我真的无法想出解决方案:/

1 个答案:

答案 0 :(得分:0)

我认为快速排序有更好的实现,这是我的评论尝试,希望能帮助你更好地记住它:

public static void quickSort(int[] theArray, int left, int right) {
    //we declare 2 variables   
    int i = left;
    int j = right;

    //we calculate a pivot, simply taking the middle value of the array
    int pivot = theArray[(left+right)/2];

    //check that i hasn't gone beyond j (i starts at 0, j starts at array.length)
    while(i<=j){
        //if here, must mean i is less-than or equal to j, so check to see if
        //the array value i is less than our pivot
        while(theArray[i] < pivot){
            //if it is less-than pivot, the value theArray[i] is in correct place
            //so we can increment i to go to next 
            i++;
        }
        //now do exactly same thing for j, however, j starts at array.length, so decrement
        while(theArray[j] > pivot){
            j--;
        }
        //if we are here, it likely means we need to swap values
        //check that i hasn't gone beyond j
        if(i<=j){
            //simple swap
            temp = theArray[i];
            theArray[i] = theArray[j];
            theArray[j] = temp;
            //we just swapped values, so we don't need to check them again, so continue
            i++; 
            j--;
        }
    }
    //now check if parameter left is < j 
    //if left has gone beyond j, it means we no longer need to further sort
    if(left<j){
        //rinse and repeat
        quickSort(theArray, left, j);
    //and check that i is still less than right parameter 
    }if(i < right){
        //rinse and repeat
        quickSort(theArray, i, right);
    }

}

<强>用法:

//you can amend this code so you don't have to pass in an array
quickSort(theArray, 0, theArray.length-1);

一旦你理解了quicksort正在尝试做什么,这很简单。不要强调它,花15分钟休息,观看算法的图形表示,并思考代码应该如何表现以及它想要实现的目标。回到这里,查看代码,然后开始实现它。冲洗并重复!祝好运!

另外(不确定你的考试布局是怎样)但你也可以提到O(n log n)足够保证运行时,你真的应该shuffle the array之前。