Java Array Sort countingSort(int[] a, int low, int high)

Here you can find the source of countingSort(int[] a, int low, int high)

Description

Counting sort that sorts the integer array in O(n+k) where n is the number of elements and k is the length of the integer intervals given (high - low).

License

Apache License

Declaration

public static void countingSort(int[] a, int low, int high) 

Method Source Code

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

import java.util.Arrays;

public class Main {
    /**/*from  w  ww  . j  av  a2  s  .  c om*/
     * Counting sort that sorts the integer array in O(n+k) where n is the number
     * of elements and k is the length of the integer intervals given (high -
     * low). So you can imagine that it uses domain knowledge of the contained
     * integers, like the lowest value and the highest. It only works for positive
     * numbers, so please don't come up with negative numbers, it will result in
     * array out of bound exceptions, since they don't have an array index.
     */
    public static void countingSort(int[] a, int low, int high) {
        final int[] counts = new int[high - low + 1];
        for (int x : a) {
            counts[x - low]++;
        }

        int current = 0;
        for (int i = 0; i < counts.length; i++) {
            Arrays.fill(a, current, current + counts[i], i + low);
            current += counts[i];
        }
    }
}

Related

  1. calculatePValueForDataPoint(double dataPoint, double[] sortedNullHypothesisSample)
  2. collectionToSortedArray(Collection objects)
  3. copyAndSort(String[] input)
  4. copyAndSort(T[] builtinFunctions)
  5. copySortArray(String[] values)
  6. countingSort(int[] array, int low, int high)
  7. deltaMetricToSortedIndicies( final double[][] deltaMetric)
  8. difference(int[] sorted1, int[] sorted2)
  9. extractFields(String[] items, int[] fields, String delim, boolean sortKeyFields)