Inplace Quicksort in Java

To update some Java, I tried to implement the quicksort (inplace) algorithm, which can sort entire arrays. Below is the code that I have received so far. You can call him sort(a,0,a.length-1).

This code obviously fails (falls into an infinite loop) if both pointers i,jpoint to each element of the array that has the same values โ€‹โ€‹as the pivot point. The pivot element is valways the right majority of the current section (with the highest index).

But I just canโ€™t figure out how to avoid this, does anyone see a solution?

static void sort(int a[], int left, int right)   {
    if (right > left){
        int i=left, j=right-1, tmp;
        int v = a[right]; //pivot
        int counter = 0;
        do {
            while(a[i]<v)i++;
            while(j>0 && a[j]>v)j--;

            if( i < j){
                tmp = a[i];
                a[i] = a[j];
                a[j] = tmp;
            }
        } while(i < j);
        tmp = a[right];
        a[right] = a[i];
        a[i] = tmp;
        sort(a,left,i-1);
        sort(a,i+1,right);

    }
}    
+4
source share
3 answers

( , !):

EDIT: . 2 , .

public static void main (String[] args) throws java.lang.Exception
{
    int b[] = {10, 9, 8, 7, 7, 7, 7, 3, 2, 1};
    sort(b,0,b.length-1);
    System.out.println(Arrays.toString(b));
}

static void sort(int a[], int left, int right)   {  
   if (right > left){
    int i=left, j=right, tmp;    
    //we want j to be right, not right-1 since that leaves out a number during recursion

    int v = a[right]; //pivot

    do {
        while(a[i]<v)
          i++;
        while(a[j]>v) 
        //no need to check for 0, the right condition for recursion is the 2 if statements below.
          j--;

        if( i <= j){            //your code was i<j
           tmp = a[i];
           a[i] = a[j];
           a[j] = tmp;
           i++;            
           j--;
           //we need to +/- both i,j, else it will stick at 0 or be same number
        }
   } while(i <= j);           //your code was i<j, hence infinite loop on 0 case

    //you had a swap here, I don't think it needed.
    //this is the 2 conditions we need to avoid infinite loops
    // check if left < j, if it isn't, it already sorted. Done

    if(left < j)  sort(a,left,j);
    //check if i is less than right, if it isn't it already sorted. Done
    // here i is now the 'middle index', the slice for divide and conquer.

    if(i < right) sort(a,i,right);
  }

}

- IDEOne

, , I/J, , , .

, , ( ), , , , , j, 0 ( ), 1.:)

, 2 , , . , , . , , j, , . , j , , j .

RosettaCode:

function quicksort(array)
    if length(array) > 1
        pivot := select any element of array
        left := first index of array
        right := last index of array
        while left โ‰ค right
            while array[left] < pivot
                left := left + 1
            while array[right] > pivot
                right := right - 1
            if left โ‰ค right
                swap array[left] with array[right]
                left := left + 1
                right := right - 1
        quicksort(array from first index to right)
        quicksort(array from left to last index)

: SO

, - oridnary while

:)

+2

Quicksort , ( ). , - , , . .

:

public static void sort (int[] a)
{
    StdRandom.shuffle(a);
    sort(a, 0, a.length - 1);
}

private static void sort(int[] a, int lo, int hi)
{
    if (hi <= lo) return;
    int j = partition(a, lo, hi) // the addition of a partitioning method
    sort(a, lo, j-1);
    sort(a, j+1, hi);
}

private static int partition(int[] a, int lo, int hi)
{
    int i = lo, j = hi + 1, tmp = 0;
    int v = a[lo];
    while (true)
    {
         while (a[i++] < v) if (i == hi) break;
         while (v < a[j--]) if (j == lo) break;
         if (i >= j) break;
         tmp = a[i];
         a[i] = a[j];
         a[j] = tmp;
    }
    tmp = a[lo];
    a[lo] = a[j];
    a[j] = temp;
    return j;
}

, , Quicksort ( ), . .

+3

Here is some simple code that I wrote that is not initialized by many pointers and does the job in a simple way.

public int[] quickSort(int[] x ){
    quickSortWorker(x,0,x.length-1);
    return x;
}


private int[] quickSortWorker(int[] x, int lb, int ub){
    if (lb>=ub) return x; 
    int pivotIndex = lb;
    for (int i = lb+1 ; i<=ub; i++){
        if (x[i]<=x[pivotIndex]){
            swap(x,pivotIndex,i);
            swap(x,i,pivotIndex+1);
            pivotIndex++;
        }
    }
    quickSortWorker(x,lb,pivotIndex-1);
    quickSortWorker(x,pivotIndex+1,ub);
    return x;
}

private void swap(int[] x,int a, int b){
    int tmp = x[a];
    x[a]=x[b];
    x[b]=tmp;
}
0
source

All Articles