Java Quick Sort quicksort(float[] a, int[] index, int left, int right)

Here you can find the source of quicksort(float[] a, int[] index, int left, int right)

Description

quicksort

License

Open Source License

Declaration

private static void quicksort(float[] a, int[] index, int left, int right) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static void quicksort(float[] a, int[] index, int left, int right) {
        if (right <= left) {
            return;
        }/*from  ww  w  .  j a  va2s  .  c om*/

        int i = partition(a, index, left, right);
        quicksort(a, index, left, i - 1);
        quicksort(a, index, i + 1, right);
    }

    private static int partition(float[] a, int[] index, int left, int right) {
        int i = left - 1;
        int j = right;
        while (true) {
            // find item on left to swap
            while (a[index[++i]] < a[index[right]])
                ; // a[right] acts as sentinel
            // find item on right to swap
            while (a[index[right]] < a[index[--j]]) {
                // don't go out-of-bounds
                if (j == left) {
                    break;
                }
            }

            // check if pointers cross
            if (i >= j) {
                break;
            }

            swap(a, index, i, j); // swap two elements into place
        }

        swap(a, index, i, right); // swap with partition element
        return i;
    }

    private static void swap(float[] a, int[] index, int i, int j) {
        int tmp = index[i];
        index[i] = index[j];
        index[j] = tmp;
    }
}

Related

  1. quickSort(double[] array, int low, int high)
  2. quickSort(double[] array, int[] idx, int from, int to)
  3. quicksort(double[] main, int[] index)
  4. quicksort(final double[] array, final int[] index)
  5. quicksort(final long[] data, final int left, final int right)
  6. quickSort(int[] a)
  7. quickSort(int[] a, int start, int end)
  8. quickSort(int[] a, int start, int end, int[] p)
  9. quickSort(int[] arr, int left, int right)