Java Quick Sort quickSort(int[] array)

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

Description

quick Sort

License

Open Source License

Declaration

public static int[] quickSort(int[] array) 

Method Source Code

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

public class Main {
    public static int[] quickSort(int[] array) {
        quickSort(null, array, 0, array.length - 1);
        return array;
    }//from w  w w  .  j  a v  a2  s  .  c om

    public static Object[] quickSort(Object[] data, int[] indexes) {
        quickSort(data, indexes, 0, indexes.length - 1);
        return data;
    }

    public static double[] quickSort(double[] array) {
        quickSort(null, array, 0, array.length - 1);
        return array;
    }

    public static Object[] quickSort(Object[] data, double[] indexes) {
        quickSort(data, indexes, 0, indexes.length - 1);
        return data;
    }

    public static void quickSort(Object[] data, int[] array, int left,
            int right) {
        int i = left, j = right;
        int temp;
        Object dataTemp;
        int pivot = array[(left + right) / 2];

        /* partition */
        while (i <= j) {
            while (array[i] < pivot)
                i++;
            while (array[j] > pivot)
                j--;

            if (i <= j) {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;

                if (data != null) {
                    dataTemp = data[i];
                    data[i] = data[j];
                    data[j] = dataTemp;
                }

                i++;
                j--;
            }
        }

        /* recursion */
        if (left < j)
            quickSort(data, array, left, j);

        if (i < right)
            quickSort(data, array, i, right);
    }

    public static void quickSort(Object[] data, double[] array, int left,
            int right) {
        int i = left, j = right;
        double temp;
        Object dataTemp;
        double pivot = array[(left + right) / 2];

        /* partition */
        while (i <= j) {
            while (array[i] < pivot)
                i++;
            while (array[j] > pivot)
                j--;

            if (i <= j) {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;

                if (data != null) {
                    dataTemp = data[i];
                    data[i] = data[j];
                    data[j] = dataTemp;
                }

                i++;
                j--;
            }
        }

        /* recursion */
        if (left < j)
            quickSort(data, array, left, j);

        if (i < right)
            quickSort(data, array, i, right);
    }
}

Related

  1. quickSort(int[] a)
  2. quickSort(int[] a, int start, int end)
  3. quickSort(int[] a, int start, int end, int[] p)
  4. quickSort(int[] arr, int left, int right)
  5. quickSort(int[] array)
  6. quicksort(int[] array)
  7. quickSort(int[] array, int lo0, int hi0)
  8. quickSort(int[] array, int[] index, int lo0, int hi0)
  9. quicksort(int[] source)