qwerty787788

Ints sort (two times faster than Arrays.sort)

Apr 18th, 2013
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1.     public static void sort(int[] a, int from, int to) {
  2.         int n = to - from;
  3.         int[] temp = new int[n];
  4.         int[] cnt = new int[1 << 16];
  5.         for (int i = to - 1; i >= from; --i) {
  6.             ++cnt[low(a[i])];
  7.         }
  8.         for (int i = 0; i < cnt.length - 1; ++i) {
  9.             cnt[i + 1] += cnt[i];
  10.         }
  11.         for (int i = to - 1; i >= from; --i) {
  12.             temp[--cnt[low(a[i])]] = a[i];
  13.         }
  14.  
  15.         Arrays.fill(cnt, 0);
  16.         for (int i = n - 1; i >= 0; --i) {
  17.             ++cnt[high(temp[i])];
  18.         }
  19.         cnt[0] += from;
  20.         for (int i = 0; i < cnt.length - 1; ++i) {
  21.             cnt[i + 1] += cnt[i];
  22.         }
  23.         for (int i = n - 1; i >= 0; --i) {
  24.             a[--cnt[high(temp[i])]] = temp[i];
  25.         }
  26.     }
  27.  
  28.     private static int high(int a) {
  29.         return (a ^ Integer.MIN_VALUE) >>> 16;
  30.     }
  31.  
  32.     private static int low(int a) {
  33.         return a & 0xFFFF;
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment