Java Quick Sort quickSort(int[] array)

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

Description

quick sort.

License

Apache License

Parameter

Parameter Description
array a parameter

Declaration

public static void quickSort(int[] array) 

Method Source Code

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

public class Main {
    /**//w w w . j av  a  2s . co m
     * quick sort.
     * 
     * @param array
     */
    public static void quickSort(int[] array) {
        quickSort(array, 0, array.length);
    }

    private static void quickSort(int[] array, int start, int end) {
        int r = 0;
        if (start < end) {
            r = partition(array, start, end);

            quickSort(array, start, r - 1);
            quickSort(array, r + 1, end);
        }

    }

    private static int partition(int[] array, int q, int p) {
        int pivot = array[q];

        int i = q;
        // through the loop, i always points to the end of the sub-array whose
        // element is <= pivot.
        for (int j = q + 1; j < p; j++) {
            if (array[j] < pivot) {
                i++;
                swap(array, i, j);
            }
        }

        swap(array, i, q);
        return i;
    }

    private static void swap(int[] array, int a, int b) {
        int temp = array[a];
        array[a] = array[b];
        array[b] = temp;
    }
}

Related

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