Java Quick Sort quicksort(int[] array)

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

Description

quicksort

License

Open Source License

Declaration

public static void quicksort(int[] array) 

Method Source Code

//package com.java2s;
/**// www.ja  va 2s  . c om
 * Copyright (c) 2014-2015 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Any modifications to this file must keep this entire header intact.
 * 
 * Change History - most recent changes go on top of previous changes
 *
 * ArrayUtils.java
 *
 * Who   Date       Description
 * ====  =========  ======================================================================
 * WY    06Apr2015  Added reverse(byte[]) to reverse byte array elements
 * WY    06Jan2015  Added reverse() to reverse array elements
 * WY    10Dec2014  Moved reverseBits() from IMGUtils to here along with BIT_REVERSE_TABLE
 * WY    08Dec2014  Fixed bug for flipEndian() with more than 32 bit sample data 
 * WY    07Dec2014  Changed method names for byte array to other array types conversion
 * WY    07Dec2014  Added new methods to work with floating point TIFF images
 * WY    03Dec2014  Added byteArrayToFloatArray() and byteArrayToDoubleArray()
 * WY    25Nov2014  Added removeDuplicates() to sort and remove duplicates from int arrays
 * WY    12Nov2014  Changed the argument sequence for flipEndian()
 * WY    11Nov2014  Changed flipEndian() to include scan line stride to skip bits
 * WY    11Nov2014  Added toNBits() to convert byte array to nBits data unit
 * WY    28Oct2014  Added flipEndian() to work with TIFTweaker mergeTiffImagesEx()
 */

public class Main {
    public static void quicksort(int[] array) {
        quicksort(array, 0, array.length - 1);
    }

    public static void quicksort(int[] array, int start, int end) {
        int inner = start;
        int outer = end;
        int mid = (start + end) / 2;

        do {
            // work in from the start until we find a swap to the
            // other partition is needed 
            while ((inner < mid) && (array[inner] <= array[mid]))
                inner++;

            // work in from the end until we find a swap to the
            // other partition is needed

            while ((outer > mid) && (array[outer] >= array[mid]))
                outer--;

            // if inner index <= outer index, swap elements   
            if (inner < mid && outer > mid) {
                swap(array, inner, outer);
                inner++;
                outer--;
            } else if (inner < mid) {
                swap(array, inner, mid - 1);
                swap(array, mid, mid - 1);
                mid--;
            } else if (outer > mid) {
                swap(array, outer, mid + 1);
                swap(array, mid, mid + 1);
                mid++;
            }
        } while (inner != outer);

        // recursion
        if ((mid - 1) > start)
            quicksort(array, start, mid - 1);
        if (end > (mid + 1))
            quicksort(array, mid + 1, end);
    }

    public static <T extends Comparable<? super T>> void quicksort(T[] array, int low, int high) {
        int i = low, j = high;
        // Get the pivot element from the middle of the list
        T pivot = array[low + (high - low) / 2];

        // Divide into two lists
        while (i <= j) {
            // If the current value from the left list is smaller then the pivot
            // element then get the next element from the left list
            while (array[i].compareTo(pivot) < 0) {
                i++;
            }
            // If the current value from the right list is larger then the pivot
            // element then get the next element from the right list
            while (array[j].compareTo(pivot) > 0) {
                j--;
            }

            // If we have found a values in the left list which is larger then
            // the pivot element and if we have found a value in the right list
            // which is smaller then the pivot element then we exchange the
            // values.
            // As we are done we can increase i and j
            if (i <= j) {
                swap(array, i, j);
                i++;
                j--;
            }
        }
        // Recursion
        if (low < j)
            quicksort(array, low, j);
        if (i < high)
            quicksort(array, i, high);
    }

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

    private static final <T extends Comparable<? super T>> void swap(T[] array, int a, int b) {
        T temp = array[a];
        array[a] = array[b];
        array[b] = temp;
    }
}

Related

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