How to sort large array faster in Java

1 Answer

0 votes
package javaapplication1;

import java.util.Arrays;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            int[] arr = new int[1000000];
            
            for (int i = 0; i < arr.length; i++) {
                arr[i] = (int)(Math.random() * 100000);
            }
            
            Arrays.parallelSort(arr);

            System.out.println(arr[0]); // first
            System.out.println(arr[1]); 
            System.out.println(arr[2]); 
            System.out.println(arr[3]); 
            
            System.out.println(arr[652]); 
            System.out.println(arr[7857]); 
            System.out.println(arr[98633]); 
            
            System.out.println(arr[arr.length - 1]); // last
            
            
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

/*
              
run:
 
0
0
0
0
74
783
9845
99999
     
 */

 



answered Dec 13, 2016 by avibootz
...