简体   繁体   中英

Faster Arrays sort function in java

I found this code snippet on codeforces.

 public static int[] sort(int[] a) {
        a = shuffle(a, new SplittableRandom());
        Arrays.sort(a);
        return a;
    }

Is this a better way to sort an array rather than using just Arrays.sort(a) . Why or Why not?

It's because Arrays.sort() uses Quicksort (Dual-Pivot Quicksort, to be more precise), which can have a complexity of O(n^2) (worst case complexity), instead of O(n log(n)), when the array has small or big values on the first/last position. In other words, Quicksort has the best efficiency when the order of the elements is completely random.

You can find a more detailed explanation here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM