Java中的Quicksort实现

时间:2014-04-09 14:44:11

标签: java algorithm quicksort

我试图实现Quicksort并且它无法正常工作。

请告诉我哪里出错了。我是否错误地实施了逻辑? 我用这组数字测试了上面的代码 - 13,26,12,15,10,15,12

public class QuickSort {

    private int array[];
    private int arrayLength;

    public void sort(int[] values) {

        if (values == null || values.length == 0)
            return;

        this.array = values;
        this.arrayLength = array.length - 1;

        quickSort(0, arrayLength);
    }

    private void quickSort(int low, int high) {
        int i = low, j = high;

            // Maximum Number of elements should be equal to arraylength
            int leftSubArray[] = new int[high+1];
            int rightSubArray[] = new int[high+1];

            int pivot = array[low];
            System.out.println("Pivot = " + pivot + " and position = " + low);

            int tempMax = low;

            // Divide the list in two parts

            // Left sublist smaller than pivot
            // Incremented by one to exclude the pivot
            while (i < high) {
                if (array[i + 1] < pivot) {
                    leftSubArray[tempMax] = array[i + 1];
                    tempMax++;
                }
                i++;
            }

            // Right sublist greater than pivot
            tempMax = j;
            while (j > low) {
                if (array[j] >= pivot) {
                    rightSubArray[tempMax] = array[j];
                    tempMax--;
                }
                j--;
            }

            // Combining both the arrays
            i = low;
            while (i <= tempMax) {
                array[i] = leftSubArray[i];
                i++;
            }

            array[tempMax] = pivot;

            // defining the limit of the next recursive call
            j = tempMax-1;
            i = low;

            tempMax++;
            while (tempMax <= high) {
                array[tempMax] = rightSubArray[tempMax];
                tempMax++;
            }

            displayArray();

            // Recursion
            if (low < j) 
                quickSort(low, j);

            tempMax++;
            if (tempMax < arrayLength)
                quickSort(tempMax, arrayLength);

    }

    private void displayArray() {
        for (int i : array) {
            System.out.print(i + ",");
        }
        System.out.println("\b\n");
    }
}

编辑: 这是Eclipse的控制台o / p:

Pivot = 13 and position = 0
12,10,12,13,26,15,15,

Pivot = 12 and position = 0
10,12,12,13,26,15,15,

Pivot = 26 and position = 4
10,12,12,13,15,15,26,

Pivot = 15 and position = 4
10,12,12,13,15,15,26,

Pivot = 15 and position = 4
10,12,12,13,15,15,0,

Pivot = 10 and position = 0
10,0,0,13,15,15,0,

Pivot = 15 and position = 4
10,0,0,13,0,15,15,

Pivot = 0 and position = 4
10,0,0,13,0,0,0,

Pivot = 10 and position = 0
0,0,10,0,0,0,0,

Pivot = 0 and position = 0
0,0,10,0,0,0,0,

Pivot = 0 and position = 3
0,0,10,0,0,0,0,

Pivot = 0 and position = 0
0,0,0,0,0,0,0,

2 个答案:

答案 0 :(得分:1)

以下是工作代码。

public class QuickSort{

int arr[] = {12,9,4,99,120,1,3,130,13};


public static void main(String args[])
{

QuickSort qs = new QuickSort();

qs.quickSort(qs.arr,0,qs.arr.length-1);
System.out.println("");


}

void quickSort(int arr[],int left,int right)
{
   int i = left, j = right;

   int tmp;int p;

   int pivot = arr[(left + right) / 2];


System.out.println("");

for(p=0;p<arr.length;p++)
{
    System.out.print(arr[p] + " ");

}System.out.println("\n\nPivot = " +pivot+" Left= "+left+" j= " +j+ " I= "+i+ " Right= "+right+"  {before entering do-while}\n");

/* partition */

  while (i <= j) {

        while (arr[i] < pivot)

              i++;

        while (arr[j] > pivot)

              j--;

        if (i <= j) {

              tmp = arr[i];

              arr[i] = arr[j];

              arr[j] = tmp;

              i++;

              j--;

        }
/*for(p=0;p<arr.length;p++)
    {
        System.out.print(arr[p]+" ");

    }
    System.out.println();*/

  }




for(p=0;p<arr.length;p++)
{
    System.out.print(arr[p]+" ");
}

System.out.println("\n\nPivot = " +pivot+" Left= "+left+" j= " +j+ " I= "+i+ " Right= "+right+" {after each do-while}");

/***********/


 /* recursion */

  if (left < j){
    System.out.println("\nInside First if Left = "+left+ " J = " +j);       

        quickSort(arr, left, j);
}

  if (i < right){
    System.out.println("\nInside Second if i = " +i+ " Right = " +right);
        quickSort(arr, i, right);
}

/*******/

} }

答案 1 :(得分:0)

Giovanni BottalinkTheodore Norvell评论的指导下,我能够核心地实施逻辑。