Java Quicksort帮助

时间:2011-06-13 03:06:46

标签: java quicksort

我正在尝试在java中实现快速排序。但是,我遇到了奇怪的行为。我的算法适用于70个或更少的项目,但高于此项目的任何内容都会冻结整个Java应用程序这是函数调用的限制还是我正在使用的内存量?

我通常不使用quicksort所以我的实现可能有点偏,但我认为一般逻辑是正确的:

int data[];

public void QuickSort(int start, int end)
{
            if(start == end || end < start || end > data.length)
            {
                return;
            }

            //find the pivot
            int pivotPos = (int)(start + (end - start) / 2);
            int temp;

            //switch the pivot to the end
            temp = data[pivotPos];
            data[pivotPos] = data[end];
            data[end] = temp;

            int LowerPoint = start;
            int HigherPoint = end - 1;

            //loop through and move low values down
            //and high values up
            while(LowerPoint != HigherPoint)
            {
                while(data[LowerPoint] < data[end] && LowerPoint < HigherPoint)
                {
                    LowerPoint++;
                }

                while(data[HigherPoint] > data[end]  && LowerPoint < HigherPoint)
                {
                    HigherPoint--;
                }

                if(LowerPoint != HigherPoint)
                {
                    temp = data[HigherPoint];
                    data[HigherPoint] = data[LowerPoint];
                    data[LowerPoint] = temp;
                }
            }

            //one last check to make sure we don't swap the pivot with a lower value
            //if this value is lower than increment our pointers up so we guarentee
            //the swap with a higher value
            if(data[LowerPoint] < data[end])
            {
                LowerPoint++;
                HigherPoint++;
            }

            //place the pivot back to the middle of the list
            //by swapping it with the higher point
            temp = data[HigherPoint];
            data[HigherPoint] = data[end];
            data[end] = temp;

            this.QuickSort(0, LowerPoint - 1);
            this.QuickSort(HigherPoint + 1, end);

        }

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

仔细看看以下两行:

    this.QuickSort(0, LowerPoint - 1);
    this.QuickSort(HigherPoint + 1, end);

对他们来说有点不对劲。

答案 1 :(得分:0)

我在上次作业中实施了快速排序,这是我的代码,它可能对您有所帮助:

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

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