Java Array Sort sortInPlace(int[] array)

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

Description

Sort array.

License

Open Source License

Parameter

Parameter Description
array array containing values from 0 to N-1, where N is length of array

Declaration

private static void sortInPlace(int[] array) 

Method Source Code

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

public class Main {
    /**//from   w w  w  .j a va  2 s  .  c o  m
     * Sort array.
     * It doesn't use extra space and do it in linear time.
     * 
     * @param array
     *       array containing values from 0 to N-1, where N is length of array
     */
    private static void sortInPlace(int[] array) {

        populateCountsFromArray(array);
        populateArrayFromCounts(array);
        return;
    }

    /**
     * @param array
     *       array containing values which are with in range from 0 to length of array
     * @param min
     *       minimum value present in array
     */
    private static void populateCountsFromArray(int[] array) {

        for (int i = 0; i < array.length; i++) {

            if (array[i] < 0) {
                continue;
            }

            int elementInHand = array[i];
            array[i] = 0;
            while (true) {
                int temp = array[elementInHand];
                if (temp < 0 || (temp == 0 && elementInHand <= i)) {
                    array[elementInHand] -= 1;
                    break;
                } else {
                    array[elementInHand] = -1;
                    elementInHand = temp;
                }
            }
        }
    }

    /**
     * Populate sorted array using the counts given in array.
     * 
     * It doesn't use extra space, same array is modified.
     * 
     * @param array
     *          array keeping count at indices i.e. if index <b>i</b> have value <b>-n</b>
     *          then there are <b>n</b> occurrences of <b>i</b> in array.
     */
    private static void populateArrayFromCounts(int[] array) {
        int nextNegative = 0;
        int count = 0;
        outerloop: for (int i = 0; i < array.length; i++) {
            while (nextNegative <= i || array[nextNegative] >= 0) {
                nextNegative++;
                if (nextNegative >= array.length)
                    break outerloop;
            }
            count++;
            count += array[i];
            if (count > 0) {
                array[i] = nextNegative;
                array[nextNegative]++;
                count--;
            }
        }

        int lastNegative = array.length - 1;
        count = 0;
        outerloop: for (int i = array.length - 1; i >= 0; i--) {

            if (array[i] > 0)
                continue;

            while (lastNegative >= i || array[lastNegative] >= 0) {
                lastNegative--;
                if (lastNegative < 0)
                    break outerloop;
            }
            count++;
            count += array[i];
            if (count > 0) {
                array[i] = lastNegative;
                array[lastNegative]++;
                count--;
            }
        }

        for (int i = 0; i < array.length; i++) {
            if (array[i] == -1) {
                array[i] = i;
            }
        }
    }
}

Related

  1. sortIndexes(final T[] array)
  2. sortIndexesDescending(final double[] in)
  3. sortindices(double[] x)
  4. sortIndices(float[] main)
  5. sortInPlace(final double[] v, final int i, final int j)
  6. sortInterval(byte[] x, int start, int end)
  7. sortKeyValuePairs(long[] keys, double[] values, int startInd, int endInd)
  8. sortLeftRightAndCenter(double[] array, int[] index, int l, int r)
  9. sortMatrixRows(A[] matrix, Comparator comparator)