當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java 8 Arrays parallelSort()用法及代碼示例


Java 8在java.util.Arrays類中引入了一種稱為parallelSort()的新方法。它使用數組元素的並行排序

parallelSort()的算法

1. The array is divided into sub-arrays and that 
   sub-arrays is again divided into their sub-arrays, 
   until the minimum level of detail in a set of array.
2. Arrays are sorted individually by multiple thread. 
3. The parallel sort uses Fork/Join Concept for sorting.
4. Sorted sub-arrays are then merged.

用法:


  1. 對於按升序排序的數據:
    public static void parallelSort(Object obj[])
    
  2. 對於指定範圍內的數據升序排序:
    public static void parallelSort(Object obj[], int from, int to)

優點:
parallelSort()方法使用MultiThreading的概念,與常規排序方法相比,它使排序更快。


下麵是將說明Arrays.parallelSort()用法的程序:

程序1:演示並行排序的使用

// Java program to demonstrate 
// Arrays.parallelSort() method 
  
import java.util.Arrays; 
  
public class ParallelSort { 
    public static void main(String[] args) 
    { 
        // Creating an array 
        int numbers[] = { 9, 8, 7, 6, 3, 1 }; 
  
        // Printing unsorted Array 
        System.out.print("Unsorted Array:"); 
        // Iterating the Elements using stream 
        Arrays.stream(numbers) 
            .forEach(n -> System.out.print(n + " ")); 
        System.out.println(); 
  
        // Using Arrays.parallelSort() 
        Arrays.parallelSort(numbers); 
  
        // Printing sorted Array 
        System.out.print("Sorted Array:"); 
        // Iterating the Elements using stream 
        Arrays.stream(numbers) 
            .forEach(n -> System.out.print(n + " ")); 
    } 
}
輸出:
Unsorted Array:9 8 7 6 3 1 
Sorted Array:1 3 6 7 8 9

時間複雜度是O(nlogn)

程序2:演示並行排序w.r.t.係列排序(普通排序)

// Java program to demonstrate impact  
// of Parallel Sort vs Serial Sort  
  
import java.util.Arrays;  
import java.util.Random;  
  
public class ParallelSort {  
    public static void main(String[] args)  
    {  
        // Creating an array  
        int numbers[] = new int[100];  
  
        // Iterating Loop till i = 1000  
        // with interval of 10  
        for (int i = 0; i < 1000; i += 10) {  
  
            System.out.println("\nFor iteration number:"
                            + (i / 10 + 1));  
  
            // Random Int Array Generation  
            Random rand = new Random();  
  
            for (int j = 0; j < 100; j++) {  
                numbers[j] = rand.nextInt();  
            }  
  
            // Start and End Time of Arrays.sort()  
            long startTime = System.nanoTime();  
  
            // Performing Serial Sort  
            Arrays.sort(numbers);  
  
            long endTime = System.nanoTime();  
  
            // Printing result of Serial Sort  
            System.out.println("Start and End Time in Serial (in ns):"
                            + startTime + ":" + endTime);  
            System.out.println("Time taken by Serial Sort(in ns):"
                            + (endTime - startTime));  
  
            // Start and End Time of Arrays.parallelSort()  
            startTime = System.nanoTime();  
  
            // Performing Parallel Sort  
            Arrays.parallelSort(numbers);  
  
            endTime = System.nanoTime();  
  
            // Printing result of Parallel Sort  
            System.out.println("Start and End Time in parallel (in ns):"
                            + startTime + ":" + endTime);  
            System.out.println("Time taken by Parallel Sort(in ns):"
                            + (endTime - startTime));  
            System.out.println();  
        }  
    }  
} 
輸出:
For iteration number:1
Start and End Time in Serial (in ns):3951000637977:3951000870361
Time taken by Serial Sort(in ns):232384
Start and End Time in parallel (in ns):3951000960823:3951000971044
Time taken by Parallel Sort(in ns):10221


For iteration number:2
Start and End Time in Serial (in ns):3951001142284:3951001201757
Time taken by Serial Sort(in ns):59473
Start and End Time in parallel (in ns):3951001256643:3951001264039
Time taken by Parallel Sort(in ns):7396
.
.
.
For iteration number:99
Start and End Time in Serial (in ns):3951050723541:3951050731520
Time taken by Serial Sort(in ns):7979
Start and End Time in parallel (in ns):3951050754238:3951050756130
Time taken by Parallel Sort(in ns):1892


For iteration number:100
Start and End Time in Serial (in ns):3951050798392:3951050804741
Time taken by Serial Sort(in ns):6349
Start and End Time in parallel (in ns):3951050828544:3951050830582
Time taken by Parallel Sort(in ns):2038

注意:將打印不同的時間間隔,但是將在正常排序之前進行並行排序。

環境:2.6 GHz Intel Core i7,Java版本8



相關用法


注:本文由純淨天空篩選整理自bilal-hungund大神的英文原創作品 Java 8 | Arrays parallelSort() method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。