Sort an array of primitives with a custom comparator and without converting to objects

前端 未结 5 1202
生来不讨喜
生来不讨喜 2021-02-07 08:07

What\'s the simplest way to sort a primitive array in Java with a custom comparator (or key) function and without converting to an array of objects (for performance †).

5条回答
  •  醉酒成梦
    2021-02-07 08:42

    In Java 8 you can have your sort method take a function interface. This is modified code from OpenJDK (Copyright 1997-2007 Sun Microsystems, Inc. GPLv2):

    import java.util.function.LongBinaryOperator;
    
    public class ArraySort {
        public static void sort(long[] x, LongBinaryOperator op) {
            sort1(x, 0, x.length, op);
        }
    
        private static void sort1(long x[], int off, int len, LongBinaryOperator op) {
            if (len < 7) {
                for (int i=off; ioff && (op.applyAsLong(x[j-1], x[j]) > 0); j--)
                        swap(x, j, j-1);
                return;
            }
    
            int m = off + (len >> 1);
            if (len > 7) {
                int l = off;
                int n = off + len - 1;
                if (len > 40) {
                    int s = len/8;
                    l = med3(x, l,     l+s, l+2*s);
                    m = med3(x, m-s,   m,   m+s);
                    n = med3(x, n-2*s, n-s, n);
                }
                m = med3(x, l, m, n);
            }
            long v = x[m];
    
            int a = off, b = a, c = off + len - 1, d = c;
            while(true) {
                // Use custom comparator for checking elements
                while (b <= c && (op.applyAsLong(x[b], v) <= 0)) {
                    if (x[b] == v)
                        swap(x, a++, b);
                    b++;
                }
                // Use custom comparator for checking elements
                while (c >= b && (op.applyAsLong(x[c], v) >= 0)) {
                    if (x[c] == v)
                        swap(x, c, d--);
                    c--;
                }
                if (b > c)
                    break;
                swap(x, b++, c--);
            }
    
            int s, n = off + len;
            s = Math.min(a-off, b-a  );  vecswap(x, off, b-s, s);
            s = Math.min(d-c,   n-d-1);  vecswap(x, b,   n-s, s);
    
            if ((s = b-a) > 1)
                sort1(x, off, s, op);
            if ((s = d-c) > 1)
                sort1(x, n-s, s, op);
        }
    
        private static void swap(long x[], int a, int b) {
            long t = x[a];
            x[a] = x[b];
            x[b] = t;
        }
    
        private static void vecswap(long x[], int a, int b, int n) {
            for (int i=0; i x[c] ? b : x[a] > x[c] ? c : a));
        }
    }
    

    And call it with lambdas or anything else implementing the LongBinaryOperator interface:

    import java.util.Arrays;
    
    public class Main {
        public static void main(String[] args) {
            long x[] = {5, 5, 7, 1, 2, 5, 8, 9, 23, 5, 32, 45, 76};
            ArraySort.sort(x, (a, b) -> b - a);         // sort descending
            System.out.println(Arrays.toString(x));
        }
    }
    

    Output:

    [76, 45, 32, 23, 9, 8, 7, 5, 5, 5, 5, 2, 1]
    

提交回复
热议问题