歸并排序的多線(xiàn)程版本

最近在學(xué)習(xí)java多線(xiàn)程,讀了《java多線(xiàn)程編程實(shí)戰(zhàn)》有一種所有的串行操作都可以寫(xiě)成并發(fā)的錯(cuò)覺(jué),所以決定練練手,因?yàn)檫f歸操作最適合并發(fā)操作(除了io等)所以寫(xiě)了一個(gè)并發(fā)版本的歸并排序,并順手?jǐn)]了個(gè)串行的歸并排序,沒(méi)想到在<1000000的數(shù)量級(jí)中竟然比系統(tǒng)提供的排序要快。show you the code

  • 首先是生成隨機(jī)數(shù)組(比較亂,也沒(méi)有優(yōu)化,不是重點(diǎn))
class SuijiArray {
    private int length = 0;
    private int index = 0;
    private int[] array = null;
    Random rand = new Random();
    SuijiArray(int le) {
        length = le;
        index = le-1;
        array = new int[length];
        for(int i = 0; i < length; i++) {
            array[i] = i+1;
        }
    }
    
    public int[] get() {
        int[] a = new int[length];
        
        for(int i = 0; i< length; i++) {
            a[i] = next();
        }
        
        return a;
    }
    
    private int next() {
        if(index == 0){
            return array[0];
        }
        int random = rand.nextInt(index);
        int result = array[random];
        swap(array,index--,random);
        return result;
    }
    private void swap(int[] a, int i, int j) {
        int t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
}```

- 然后是一般的歸并排序

   public static void insertSort(int[] a,int start, int end) {
    for(int i = start + 1; i < end; i++) {
        int t = a[i];
        int j = i;
        for(; j > start && t < a[j-1]; j--) {
            a[j] = a[j-1];
        }
        a[j] = t;
    }
}


public static void mergeSort(int[] a, int start, int end){
    //在數(shù)組筆記小的時(shí)候,切換到插入排序,提供性能
    if((end - start) <= INSERT_SORT_LENGTH ) {
        insertSort(a, start, end);
    } else {
        int mid = (start + end) /2;
        mergeSort(a,start,mid);
        mergeSort(a, mid, end);
        merge(a, start, mid, end);
    }
}

public static void merge(int[] a, int start, int mid, int end) {
    int[] b= new int[end-start];
    
    int k = 0;
    int i = start ;
    int j = mid;
    
    while(i < mid && j < end) {
        if(a[i] < a[j]) {
            b[k++] = a[i++];
        }else {
            b[k++] = a[j++];
        }
    }
    
    if(i == mid) {
        while(j < end) {
            b[k++] = a[j++];
        }
    }else {
        while(i < mid) {
            b[k++] = a[i++];
        }
    }
    
    for(int ii = 0; ii < (end - start);ii++) {
        a[start + ii] = b[ii];
    }
    
}```
  • 多線(xiàn)程
public class ForkAndJoinTest extends RecursiveTask{
    public static final int INSERT_SORT_LENGTH = 23;
    private static final long serialVersionUID = 1L;
    private int[] array = null;
    private int start;
    private int end;
    
    ForkAndJoinTest(int[] a, int s, int e) {
        array = a;
        start = s;
        end = e;
    }
    

    protected Object compute() {
        boolean canCompute = (end - start) <= INSERT_SORT_LENGTH;
        if(canCompute){
            insertSort(array, start, end);
        }else{
            
            int mid = (end + start)/2;
            ForkAndJoinTest left = new ForkAndJoinTest(array,start,mid);
            ForkAndJoinTest right = new ForkAndJoinTest(array,mid,end);
            
            left.fork();
            right.fork();
            
            left.join();
            right.join();
            
            merge(array,start,mid,end);
        }
        
        return null;
    }
}
  • 測(cè)試數(shù)據(jù)
public static int[] deepCopy(int[] a) {
        int b[] = new int[a.length];
        for(int i = 0; i <  a.length; i++) {
            b[i] = a[i];
        }
        return b;
    }
    
    public static void main(String[] args) {

        ForkJoinPool forkJoinPool =new ForkJoinPool(6);
        
        int[] array = new SuijiArray(50000).get();
        int[] barray = deepCopy(array);
        int[] carray = deepCopy(array);
        ForkAndJoinTest test = new ForkAndJoinTest(array,0,array.length);
        long end = 0l;
        long start = System.currentTimeMillis();
        Future future = forkJoinPool.submit(test); 
        try {
            future.get();
        end = System.currentTimeMillis();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        
        System.out.println("concurrentMergeSort:"+(end-start));
        
        long start1 = System.currentTimeMillis();
        mergeSort(barray, 0, barray.length);
        long end1 = System.currentTimeMillis();
        System.out.println("mergeSortTime=:"+(end1 - start1));
        
        long start2 = System.currentTimeMillis();
        Arrays.sort(carray);
        long end2 = System.currentTimeMillis();
        System.out.println("SystemSortTime=:"+(end2 - start2));
        
    }```
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容