JAVA同時処理経験(四)並列モードとアルゴリズム5:並列ソートモード-パリティソート

3793 ワード

一、前言
多くのコンピュータ専門の学生たちは、アルゴリズムを学ぶ最初のソートがバブルだと信じています.バブルはシリアルソートに属しています.このセクションでは、並列のいくつかの列方法について考えます.頭の穴を開けて
二、並列並べ替え
2.1バブルソート
中の解釈はもうはっきりしている.以前、授業中、意味が読めて、コードが読めませんでした.今みんなはやはり先に基礎がlを復習します
package pattern.sort;

/**
 * Created by ycy on 16/1/16.
 *   
 *       ,      
 *   :    ,       ,       
 *      ; 
 *                   
 *                  
 *
 */
public class bubble {
    public static void main(String[] args) {
        int[] arr={1,3336,7,88,454,7556};
        for (int i = arr.length-1; i>0 ; i--) {
            //1'          ,  2'              ,
            //           ,2'    ,             (           )
            for (int j = 0; j <i ; j++) {
                //2'        j j+1  ,    j+1        length  ;
                // -----                
                if (arr[j]>arr[j+1]){
                    int temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }

        for (int arrone:arr
             ) {
            System.out.println(arrone);
        }
    }
}

2.2パリティソート
パリティ:ソートキー、奇数は次のデータと交換されます.次に偶数に入り、後ろのデータと交換します.このようにデータのすべてのデータのインタラクションを完了します.
    public static void oddEnventSort(int arr[]){
        int exchflag=1,start=0;
        while (exchflag==1||start==1){
            //         
            exchflag=0;
            for (int i = start; i <arr.length ; i+=2) {
                if (arr[i] > arr[i+1]) {
                    int temp=arr[i];
                    arr[i]=arr[i+1];
                    arr[i+1]=temp;
                    exchflag=1;

                }
            }
            //       ,     0,      ;1      
            if (start==0){
                start=1;

            }else {
                start=0;
            }
        }
    }

2.2パリティソートの並列改造
  /////////////////////////        ///////////////////////////
    static int exchangeFlag=1;
    static ExecutorService pool= Executors.newCachedThreadPool();
    static int[] array={1,4,2,6,35,3};

    static synchronized void setExchangeFlag(int v){
        exchangeFlag=v;
    }
    static synchronized int getExchangeFlag(){
        return exchangeFlag;
    }
    public static class OddEventSortTask implements  Runnable{
        int i;
        CountDownLatch latch;
        public OddEventSortTask(int i,CountDownLatch latch){
            this.i=i;
            this.latch=latch;
        }


        public void run() {
            if (array[i]>array[i+1]){
                int temp=array[i];
                array[i]=array[i+1];
                array[i+1]=temp;
                setExchangeFlag(1);
            }
            latch.countDown();
        }
    }
    public static void pOddEventSort(int[] arr) throws InterruptedException {
        int start=0;
        while (getExchangeFlag()==1||start==1){
            setExchangeFlag(0);
            //       , start=1  ,  len/2-1    
            CountDownLatch latch=new CountDownLatch(arr.length/2-(arr.length%2==0?start:0));
            for (int i = start; i < arr.length; i+=2) {
                pool.submit(new OddEventSortTask(i,latch));
            }
            //        
            latch.await();
            if (start==0){
                start=1;
            }else {
                start=0;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        pOddEventSort(array);
        for (int ar:array
             ) {
            System.out.println(ar);
        }
    }
べ えの はpOddEventSort()メソッドであり、ContLatchを してスレッド を し、 ごとに、 のスレッド の と を する. にお を するには、 の ですべてのスレッドが しなければなりません.