简体   繁体   English

对随机整数数组进行排序的最快方法

[英]The fastest way to sort the array of the random integers

I have an array which consists of 10 random integers, what will be the fastest way to sort it from the largest to the smallest?我有一个由 10 个随机整数组成的数组,将它从最大到最小排序的最快方法是什么?

public class Main {
    public static void main(String args[])
    {
        int [] array = new int[10];

        array[0] = ((int)(Math.random()*100+1));
        array[1] = ((int)(Math.random()*100+1));
        array[2] = ((int)(Math.random()*100+1));
        array[3] = ((int)(Math.random()*100+1));
        array[4] = ((int)(Math.random()*100+1));
        array[5] = ((int)(Math.random()*100+1));
        array[6] = ((int)(Math.random()*100+1));
        array[7] = ((int)(Math.random()*100+1));
        array[8] = ((int)(Math.random()*100+1));
        array[9] = ((int)(Math.random()*100+1));
        
        for (int i = 0; i < 10; i++){
            System.out.println(i + ") " + array[i]);
        }
    }
}

The only thing that comes to mind is bubble sort and the insertion sort唯一想到的就是冒泡排序和插入排序

For ascending order it would be as easy as:对于升序,它会很简单:

Arrays.sort(array); Arrays.排序(数组);

Unfortunately due to the unboxing feature, it's a bit more complicated but still doable.不幸的是,由于拆箱功能,它有点复杂但仍然可行。

    array = Arrays.stream(array)   // turn into an IntStream
           .boxed()                // Then into a Stream<Integer>
           .sorted((a,b) -> b - a) // Sort descending
           .mapToInt(i -> i)       // Convert Integer back to int
           .toArray();             // Convert back to int[]

Although there are several ways to do this, this is one way.尽管有多种方法可以做到这一点,但这是一种方法。 Read the comments in code:阅读代码中的注释:

int [] array = new int[10];

/* Generate an int[] Array with 10 elements consisting 
   of 10 elements holding a random number from 1 to 100
   (duplicates allowed).                  */
System.out.println("Generated Array (Random Order):");
// Display the generated Array...
for (int i = 0; i < array.length; i++){
    array[i] = ((int)(Math.random()*100+1));
    System.out.printf("%2d) %-6d%n", (i+1), array[i]);
}
    
System.out.println();
    
// Sort the Array in Ascending Order 'first'!
System.out.println("Sorted Array (Ascending Order):");
Arrays.sort(array);  // Sort in ascending order
// Display the sorted Array...
for (int i = 0; i < array.length; i++){
    System.out.printf("%2d) %-6d%n",(i+1), array[i]);
}
    
System.out.println();
    
// Sort the Array in Ddescending Order 'second'!
System.out.println("Sorted Array (Descending Order):");
// Create a new int[] Array (arr[]) and place array[] into it in Descending order.
int[] arr = IntStream.rangeClosed(1, array.length).map(i -> array[array.length - i]).toArray();
// Copy the arr[] array into the array[] Array to overwrite it.        
System.arraycopy(arr, 0, array, 0, arr.length);
// Display the sorted Array...
for (int i = 0; i < arr.length; i++){
    System.out.printf("%2d) %-6d%n",(i+1), array[i]);
}

When the code is run, you could see something similar to this:运行代码时,您会看到类似以下内容:

Generated Array (Random Order):
 1) 80    
 2) 54    
 3) 100   
 4) 96    
 5) 71    
 6) 87    
 7) 22    
 8) 60    
 9) 67    
10) 18    

Sorted Array (Ascending Order):
 1) 18    
 2) 22    
 3) 54    
 4) 60    
 5) 67    
 6) 71    
 7) 80    
 8) 87    
 9) 96    
10) 100   

Sorted Array (Descending Order):
 1) 100   
 2) 96    
 3) 87    
 4) 80    
 5) 71    
 6) 67    
 7) 60    
 8) 54    
 9) 22    
10) 18    

You can flip the bits of integers to sort in ascending order, then flip the bits again to sort in descending order.您可以翻转整数位以按升序排序,然后再次翻转位以按降序排序。

array = IntStream.of(array).map(i -> ~i).sorted().map(i -> ~i).toArray();

for (int i = 0; i < 10; i++) {
    System.out.println(i + ") " + array[i]);
}

output: output:

0) 100
1) 92
2) 85
3) 77
4) 64
5) 62
6) 44
7) 37
8) 22
9) 9

Since everything is positive in this case, you can do the same thing with sign reversal instead of bit reversal.由于在这种情况下一切都是积极的,你可以用符号反转而不是位反转来做同样的事情。

array = IntStream.of(array).map(i -> -i).sorted().map(i -> -i).toArray();

Note that numbers containing Integer.MIN_VALUE can't be sorted this way, because they are themselves when they are sign-reversed.请注意,包含Integer.MIN_VALUE的数字不能以这种方式排序,因为它们在符号反转时是它们自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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