Java Array Sort radixSort(int[] vs)

Here you can find the source of radixSort(int[] vs)

Description

radix Sort

License

Open Source License

Declaration

public static void radixSort(int[] vs) 

Method Source Code


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

import java.util.*;

public class Main {

    public static void radixSort(int[] vs) {
        int n = vs.length;
        int[] us = new int[n];
        int[] num = new int[1 << 8];
        for (int i = 0; i < n; i++)
            vs[i] ^= 1 << 31;//from   w ww .  ja v a 2s  .c  om
        for (int i = 0; i < 32; i += 8) {
            Arrays.fill(num, 0);
            for (int j = 0; j < n; j++) {
                num[vs[j] >>> i & 0xff]++;
            }
            for (int j = 0; j < num.length - 1; j++)
                num[j + 1] += num[j];
            for (int j = n - 1; j >= 0; j--) {
                int p = vs[j] >>> i & 0xff;
                us[--num[p]] = vs[j];
            }
            int[] t = vs;
            vs = us;
            us = t;
        }
        for (int i = 0; i < n; i++)
            vs[i] ^= 1 << 31;
    }

    public static void radixSort(long[] vs) {
        int n = vs.length;
        long[] us = new long[n];
        int[] num = new int[1 << 8];
        for (int i = 0; i < n; i++)
            vs[i] ^= 1L << 63;
        for (int i = 0; i < 64; i += 8) {
            Arrays.fill(num, 0);
            for (int j = 0; j < n; j++) {
                num[(int) (vs[j] >>> i & 0xff)]++;
            }
            for (int j = 0; j < num.length - 1; j++)
                num[j + 1] += num[j];
            for (int j = n - 1; j >= 0; j--) {
                int p = (int) (vs[j] >>> i & 0xff);
                us[--num[p]] = vs[j];
            }
            long[] t = vs;
            vs = us;
            us = t;
        }
        for (int i = 0; i < n; i++)
            vs[i] ^= 1L << 63;
    }

    public static void fill(int[][] A, int a) {
        for (int i = 0; i < A.length; i++)
            Arrays.fill(A[i], a);
    }
}

Related

  1. getNewSortedIntArray(int[] codePointArray)
  2. getSortedDistinct(int[] values)
  3. intArrayToShortArraySorted(int[] source)
  4. multiQuickSort(int[]... arrays)
  5. quickSort(int[] arr, int startIndex, int endIndex)
  6. selectionSort(int[] arr)
  7. sort( final Item[] values, final Item[] auxiliary, final int first, final int last)
  8. sort( float[] array)
  9. sort(byte[] b, int pos)