Java Array Sort bitReversalSort(final short[] real)

Here you can find the source of bitReversalSort(final short[] real)

Description

bit Reversal Sort

License

Open Source License

Declaration

private static final void bitReversalSort(final short[] real) 

Method Source Code

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

import java.util.Arrays;
import java.util.Comparator;

public class Main {
    private static Comparator<int[]> intPairComparator = new Comparator<int[]>() {
        @Override/*from   w  w w.ja  va 2s  .c o m*/
        public int compare(final int[] i, int[] j) {
            return i[1] - j[1];
        }
    };

    private static final void bitReversalSort(final short[] real) {
        /* First, make a tranlation map from the index of the array to the integer it points to. */
        final int mapping[][] = new int[real.length][2];

        /* TODO - cache / memoize this mapping for a particular size array? */
        for (int i = 0; i < mapping.length; ++i) {
            mapping[i][0] = i;
            mapping[i][1] = bitReverse31(i);
        }

        Arrays.sort(mapping, intPairComparator);

        for (int i = 0; i < real.length; ++i) {
            final int j = mapping[i][0];
            final short tmp = real[i];
            real[i] = real[j];
            real[j] = tmp;
        }
    }

    /**
     * Reverse the 31 low order bits used to comprise the integer i.
     *
     * The highorder bit is ignored because it is the signed bit.
     *
     * @param i The integer to reverse the bits of.
     *
     * @return The reversed bits.
     */
    private static final int bitReverse31(int i) {
        return ((i >>> 30) & 0x00000001) | ((i << 30) & 0x40000000)
                | ((i >>> 28) & 0x00000002) | ((i << 28) & 0x20000000)
                | ((i >>> 26) & 0x00000004) | ((i << 26) & 0x10000000)
                | ((i >>> 24) & 0x00000008) | ((i << 24) & 0x08000000)
                | ((i >>> 22) & 0x00000010) | ((i << 22) & 0x04000000)
                | ((i >>> 20) & 0x00000020) | ((i << 20) & 0x02000000)
                | ((i >>> 18) & 0x00000040) | ((i << 18) & 0x01000000)
                | ((i >>> 16) & 0x00000080) | ((i << 16) & 0x00800000)
                | ((i >>> 14) & 0x00000100) | ((i << 14) & 0x00400000)
                | ((i >>> 12) & 0x00000200) | ((i << 12) & 0x00200000)
                | ((i >>> 10) & 0x00000400) | ((i << 10) & 0x00100000)
                | ((i >>> 8) & 0x00000800) | ((i << 8) & 0x00080000)
                | ((i >>> 6) & 0x00001000) | ((i << 6) & 0x00040000)
                | ((i >>> 4) & 0x00002000) | ((i << 4) & 0x00020000)
                | ((i >>> 2) & 0x00004000) | ((i << 2) & 0x00010000);
    }
}

Related

  1. addSorted(int[] array, int[] newValues)
  2. addSortedUnique(T[] array, T object, int start)
  3. addToSortedIntArray(int[] a, int value)
  4. calculatePValueForDataPoint(double dataPoint, double[] sortedNullHypothesisSample)
  5. collectionToSortedArray(Collection objects)
  6. copyAndSort(String[] input)
  7. copyAndSort(T[] builtinFunctions)