Question

I have tried to implement Quicksort and it's not working properly.

Please tell me where I went wrong. Have I implemented the logic incorrectly? I tested the above code with these set of numbers - 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");
    }
}

EDIT: Here is the console o/p from Eclipse:

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,
Was it helpful?

Solution 2

With the guidance from Giovanni Botta's link and Theodore Norvell's comment I am able to implement the logic corectly.

OTHER TIPS

The below is the working code.

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

/*******/

} }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top