Advertisement
Guest User

sort 40M float pairs lexicographically

a guest
Nov 18th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.74 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. /*
  4.  
  5. Generation time: 1028 msec
  6. Sort time: 10854 msec
  7.  
  8. */
  9.  
  10.  
  11. public class Sort40M {
  12.  
  13.     public static void sort(int fromIndex, int toIndex) {
  14.         sort2(fromIndex, toIndex);
  15.     }
  16.  
  17.     static float compare(int ix1, int ix2) {
  18.         float v1 = a[ix1*2];
  19.         float v2 = a[ix2*2];
  20.         if (v1 == v2) {
  21.             return a[ix1*2+1] - a[ix2*2+1];
  22.         } else {
  23.             return a[ix1*2] - a[ix2*2];
  24.         }
  25.     }
  26.  
  27.     static float compare2(int ix1, float v2a, float v2b) {
  28.         float v1 = a[ix1*2];
  29.         if (v1 == v2a) {
  30.             return a[ix1*2+1] - v2b;
  31.         } else {
  32.             return a[ix1*2] - v2a;
  33.         }
  34.     }
  35.  
  36.     private static void sort2(int fromIndex, int toIndex) {
  37.         final int NEG_ZERO_BITS = Float.floatToIntBits(-0.0f);
  38.         /*
  39.          * The sort is done in three phases to avoid the expense of using
  40.          * NaN and -0.0 aware comparisons during the main sort.
  41.          */
  42.  
  43.         /*
  44.          * Preprocessing phase:  Move any NaN's to end of array, count the
  45.          * number of -0.0's, and turn them into 0.0's.
  46.          */
  47.         int numNegZeros = 0;
  48.         int i = fromIndex, n = toIndex;
  49.  
  50.         // Main sort phase: quicksort everything but the NaN's
  51.         sort1(fromIndex, n - fromIndex);
  52.  
  53.     }
  54.  
  55.     // Like public version, but without range checks.
  56.     private static int binarySearch0(int fromIndex, int toIndex,
  57.                                      float key) {
  58.         int low = fromIndex;
  59.         int high = toIndex - 1;
  60.  
  61.         while (low <= high) {
  62.             int mid = (low + high) >>> 1;
  63.             float midVal = a[mid];
  64.  
  65.             int cmp;
  66.             if (midVal < key) {
  67.                 cmp = -1;   // Neither val is NaN, thisVal is smaller
  68.             } else if (midVal > key) {
  69.                 cmp = 1;    // Neither val is NaN, thisVal is larger
  70.             } else {
  71.                 int midBits = Float.floatToIntBits(midVal);
  72.                 int keyBits = Float.floatToIntBits(key);
  73.                 cmp = (midBits == keyBits ? 0 : // Values are equal
  74.                         (midBits < keyBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
  75.                                 1));                     // (0.0, -0.0) or (NaN, !NaN)
  76.             }
  77.  
  78.             if (cmp < 0)
  79.                 low = mid + 1;
  80.             else if (cmp > 0)
  81.                 high = mid - 1;
  82.             else
  83.                 return mid; // key found
  84.         }
  85.         return -(low + 1);  // key not found.
  86.     }
  87.  
  88.     /**
  89.      * Sorts the specified sub-array of floats into ascending order.
  90.      */
  91.     private static void sort1(int off, int len) {
  92.         // Insertion sort on smallest arrays
  93.         if (len < 7) {
  94.             for (int i = off; i < len + off; i++)
  95.                 for (int j = i; j > off && a[j - 1] > a[j]; j--)
  96.                     swap(j, j - 1);
  97.             return;
  98.         }
  99.  
  100.         // Choose a partition element, v
  101.         int m = off + (len >> 1);       // Small arrays, middle element
  102.         if (len > 7) {
  103.             int l = off;
  104.             int n = off + len - 1;
  105.             if (len > 40) {        // Big arrays, pseudomedian of 9
  106.                 int s = len / 8;
  107.                 l = med3(a, l, l + s, l + 2 * s);
  108.                 m = med3(a, m - s, m, m + s);
  109.                 n = med3(a, n - 2 * s, n - s, n);
  110.             }
  111.             m = med3(a, l, m, n); // Mid-size, med of 3
  112.         }
  113.         float v = a[m*2];
  114.         float v1 = a[m*2+1];
  115.  
  116.         // Establish Invariant: v* (<v)* (>v)* v*
  117.         int aa = off, bb = aa, cc = off + len - 1, dd = cc;
  118.         while (true) {
  119.             while (bb <= cc && compare2(bb, v, v1) <= 0) {
  120.                 if (compare2(bb, v, v1) == 0)
  121.                     swap(aa++, bb);
  122.                 bb++;
  123.             }
  124.             while (cc >= bb && compare2(cc, v, v1) >= 0) {
  125.                 if (compare2(cc, v, v1) == 0)
  126.                     swap(cc, dd--);
  127.                 cc--;
  128.             }
  129.             if (bb > cc)
  130.                 break;
  131.             swap(bb++, cc--);
  132.         }
  133.  
  134.         // Swap partition elements back to middle
  135.         int s, n = off + len;
  136.         s = Math.min(aa - off, bb - aa);
  137.         vecswap(off, bb - s, s);
  138.         s = Math.min(dd - cc, n - dd - 1);
  139.         vecswap(bb, n - s, s);
  140.  
  141.         // Recursively sort non-partition-elements
  142.         if ((s = bb - aa) > 1)
  143.             sort1(off, s);
  144.         if ((s = dd - cc) > 1)
  145.             sort1(n - s, s);
  146.     }
  147.  
  148.     private static void swap(int aa, int bb) {
  149.         float t = a[aa*2];
  150.         a[aa*2] = a[bb*2];
  151.         a[bb*2] = t;
  152.         t = a[aa*2+1];
  153.         a[aa*2+1] = a[bb*2+1];
  154.         a[bb*2+1] = t;
  155.     }
  156.  
  157.     private static void vecswap(int aa, int bb, int n) {
  158.         for (int i = 0; i < n; i++, aa++, bb++)
  159.             swap(aa, bb);
  160.     }
  161.  
  162.  
  163.     private static int med3(float x[], int a, int b, int c) {
  164.         return (compare(a, b) < 0 ?
  165.                 (compare(b, c) < 0 ? b : compare(a, c) < 0 ? c : a) :
  166.                 (compare(b, c) > 0 ? b : compare(a, c) > 0 ? c : a));
  167.     }
  168.  
  169.     public static final int M = 40000000;
  170.     public static float a[] = new float[M * 2];
  171.  
  172.     public static void main(String[] args) {
  173.         long l = System.currentTimeMillis();
  174.         Random random = new Random(0);
  175.         final float[] d = a;
  176.         for (int i = 0; i < M; i++) {
  177.             d[2 * i] = random.nextFloat();
  178.             d[2 * i + 1] = random.nextFloat();
  179.         }
  180.         l = System.currentTimeMillis() - l;
  181.         System.out.println("Generation time: " + l + " msec");
  182.         l = System.currentTimeMillis();
  183.         sort(0, M-1);
  184.         l = System.currentTimeMillis() - l;
  185.         System.out.println("Sort time: " + l + " msec");
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement