簡體   English   中英

對隨機整數數組進行排序的最快方法

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

我有一個由 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]);
        }
    }
}

唯一想到的就是冒泡排序和插入排序

對於升序,它會很簡單:

Arrays.排序(數組);

不幸的是,由於拆箱功能,它有點復雜但仍然可行。

    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[]

盡管有多種方法可以做到這一點,但這是一種方法。 閱讀代碼中的注釋:

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]);
}

運行代碼時,您會看到類似以下內容:

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    

您可以翻轉整數位以按升序排序,然后再次翻轉位以按降序排序。

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:

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

由於在這種情況下一切都是積極的,你可以用符號反轉而不是位反轉來做同樣的事情。

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

請注意,包含Integer.MIN_VALUE的數字不能以這種方式排序,因為它們在符號反轉時是它們自己。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM