Java Quick Sort quickSort(int[] a)

Here you can find the source of quickSort(int[] a)

Description

Quicksorts this array.

License

Apache License

Declaration

public static void quickSort(int[] a) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from   w  ww  .j  av a 2  s.  c om*/
     * Quicksorts this array.
     */
    public static void quickSort(int[] a) {
        quickSort(a, 0, a.length);
    }

    /**
     * Quicksorts this array. With offset and length, this will be recursively
     * called by itself.
     */
    public static void quickSort(int[] a, int offset, int length) {
        if (offset < length) {
            int pivot = partition(a, offset, length);
            quickSort(a, offset, pivot);
            quickSort(a, pivot + 1, length);
        }
    }

    /**
     * Quicksorts this array.
     */
    public static void quickSort(long[] a) {
        quickSort(a, 0, a.length);
    }

    /**
     * Quicksorts this array. With offset and length, this will be recursively
     * called by itself.
     */
    public static void quickSort(long[] a, int offset, int length) {
        if (offset < length) {
            int pivot = partition(a, offset, length);
            quickSort(a, offset, pivot);
            quickSort(a, pivot + 1, length);
        }
    }

    /**
     * Quicksorts this array.
     */
    public static void quickSort(double[] a) {
        quickSort(a, 0, a.length);
    }

    /**
     * Quicksorts this array. With offset and length, this will be recursively
     * called by itself.
     */
    public static void quickSort(double[] a, int offset, int length) {
        if (offset < length) {
            int pivot = partition(a, offset, length);
            quickSort(a, offset, pivot);
            quickSort(a, pivot + 1, length);
        }
    }

    /**
     * Quicksorts this array.
     */
    public static <T extends Comparable<T>> void quickSort(T[] a) {
        quickSort(a, 0, a.length);
    }

    /**
     * Quicksorts this array. With offset and length, this will be recursively
     * called by itself.
     */
    public static <T extends Comparable<T>> void quickSort(T[] a, int offset, int length) {
        if (offset < length) {
            int pivot = partition(a, offset, length);
            quickSort(a, offset, pivot);
            quickSort(a, pivot + 1, length);
        }
    }

    /**
     * Partitions the given array in-place and uses the last element as pivot,
     * everything less than the pivot will be placed left and everything greater
     * will be placed right of the pivot. It returns the index of the pivot
     * element after partitioning.
     */
    public static <T extends Comparable<T>> int partition(T[] array) {
        return partition(array, 0, array.length);
    }

    /**
     * Partitions the given array in-place and uses the end element as pivot,
     * everything less than the pivot will be placed left and everything greater
     * will be placed right of the pivot. It returns the index of the pivot
     * element after partitioning.
     */
    public static <T extends Comparable<T>> int partition(T[] array, int start, int end) {
        final int ending = end - 1;
        final T x = array[ending];
        int i = start - 1;
        for (int j = start; j < ending; j++) {
            if (array[j].compareTo(x) < 0) {
                i++;
                swap(array, i, j);
            }
        }
        i++;
        swap(array, i, ending);
        return i;
    }

    /**
     * Partitions the given array in-place and uses the last element as pivot,
     * everything less than the pivot will be placed left and everything greater
     * will be placed right of the pivot. It returns the index of the pivot
     * element after partitioning.
     */
    public static int partition(int[] array) {
        return partition(array, 0, array.length);
    }

    /**
     * Partitions the given array in-place and uses the end element as pivot,
     * everything less than the pivot will be placed left and everything greater
     * will be placed right of the pivot. It returns the index of the pivot
     * element after partitioning.
     */
    public static int partition(int[] array, int start, int end) {
        final int ending = end - 1;
        final int x = array[ending];
        int i = start - 1;
        for (int j = start; j < ending; j++) {
            if (array[j] <= x) {
                i++;
                swap(array, i, j);
            }
        }
        i++;
        swap(array, i, ending);
        return i;
    }

    /**
     * Partitions the given array in-place and uses the last element as pivot,
     * everything less than the pivot will be placed left and everything greater
     * will be placed right of the pivot. It returns the index of the pivot
     * element after partitioning.
     */
    public static int partition(long[] array) {
        return partition(array, 0, array.length);
    }

    /**
     * Partitions the given array in-place and uses the end element as pivot,
     * everything less than the pivot will be placed left and everything greater
     * will be placed right of the pivot. It returns the index of the pivot
     * element after partitioning.
     */
    public static int partition(long[] array, int start, int end) {
        final int ending = end - 1;
        final long x = array[ending];
        int i = start - 1;
        for (int j = start; j < ending; j++) {
            if (array[j] <= x) {
                i++;
                swap(array, i, j);
            }
        }
        i++;
        swap(array, i, ending);
        return i;
    }

    /**
     * Partitions the given array in-place and uses the last element as pivot,
     * everything less than the pivot will be placed left and everything greater
     * will be placed right of the pivot. It returns the index of the pivot
     * element after partitioning.
     */
    public static int partition(double[] array) {
        return partition(array, 0, array.length);
    }

    /**
     * Partitions the given array in-place and uses the end element as pivot,
     * everything less than the pivot will be placed left and everything greater
     * will be placed right of the pivot. It returns the index of the pivot
     * element after partitioning.
     */
    public static int partition(double[] array, int start, int end) {
        final int ending = end - 1;
        final double x = array[ending];
        int i = start - 1;
        for (int j = start; j < ending; j++) {
            if (array[j] <= x) {
                i++;
                swap(array, i, j);
            }
        }
        i++;
        swap(array, i, ending);
        return i;
    }

    /**
     * Swaps the given indices x with y in the array.
     */
    public static void swap(int[] array, int x, int y) {
        int tmpIndex = array[x];
        array[x] = array[y];
        array[y] = tmpIndex;
    }

    /**
     * Swaps the given indices x with y in the array.
     */
    public static void swap(long[] array, int x, int y) {
        long tmpIndex = array[x];
        array[x] = array[y];
        array[y] = tmpIndex;
    }

    /**
     * Swaps the given indices x with y in the array.
     */
    public static void swap(double[] array, int x, int y) {
        double tmpIndex = array[x];
        array[x] = array[y];
        array[y] = tmpIndex;
    }

    /**
     * Swaps the given indices x with y in the array.
     */
    public static void swap(boolean[] array, int x, int y) {
        boolean tmpIndex = array[x];
        array[x] = array[y];
        array[y] = tmpIndex;
    }

    /**
     * Swaps the given indices x with y in the array.
     */
    public static <T> void swap(T[] array, int x, int y) {
        T tmpIndex = array[x];
        array[x] = array[y];
        array[y] = tmpIndex;
    }
}

Related

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