Java Array Sort sort(int[] array)

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

Description

sort

License

Open Source License

Declaration

public static void sort(int[] array) 

Method Source Code

//package com.java2s;
/*/*w ww  .j  a  v a 2s  .c  o  m*/
 * This program is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 */

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

    private static void quickSort(int[] array, int lo0, int hi0) {
        int lo = lo0;
        int hi = hi0;
        int mid;

        if (hi0 > lo0) {
            mid = array[(lo0 + hi0) / 2];

            while (lo <= hi) {
                while ((lo < hi0) && (array[lo] < mid))
                    ++lo;
                while ((hi > lo0) && (array[hi] > mid))
                    --hi;

                if (lo <= hi) {
                    swap(array, lo, hi);
                    ++lo;
                    --hi;
                }
            }

            if (lo0 < hi)
                quickSort(array, lo0, hi);
            if (lo < hi0)
                quickSort(array, lo, hi0);
        }
    }

    public static void swap(final Object[] array, int pos) {
        swap(array, pos, pos + 1);
    }

    public static void swap(final Object[] array, int pos, boolean forward) {
        int pos2 = (forward) ? pos + 1 : pos - 1;
        swap(array, pos, pos2);
    }

    public static void swap(final Object[] array, int pos, int pos2) {
        if (pos == pos2)
            return;

        Object temp = array[pos];
        array[pos] = array[pos2];
        array[pos2] = temp;
    }

    public static void swap(final int[] array, int pos, int pos2) {
        if (pos == pos2)
            return;

        int temp = array[pos];
        array[pos] = array[pos2];
        array[pos2] = temp;
    }
}

Related

  1. sort(int[] array)
  2. sort(int[] array)
  3. sort(int[] array)
  4. sort(int[] array)
  5. sort(int[] array)
  6. sort(int[] asd)
  7. sort(int[] idxs, double[] values)
  8. Sort(int[] in)
  9. sort(int[] intValues)