奇偶交換排序

/**
 * 冒泡排序
 */
public class MoPo {
    public static void main(String[] args){
        Integer[] arr = {5,5,2,6,3,4};
        for (Integer integer : arr) {
            System.out.print(integer+" ");
        }
        for (int i = arr.length-1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (arr[j]>arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    System.out.println();
                    for (Integer integer : arr) {
                        System.out.print(integer+" ");
                    }
                }
            }
        }
        System.out.println();
        for (Integer integer : arr) {
            System.out.print(integer+" ");
        }
    }
    //5 5 2 6 3 4
    //5 2 5 6 3 4
    //5 2 5 3 6 4
    //5 2 5 3 4 6
    //2 5 5 3 4 6
    //2 5 3 5 4 6
    //2 5 3 4 5 6
    //2 3 5 4 5 6
    //2 3 4 5 5 6
    //2 3 4 5 5 6
}
/**
 * 奇偶交換排序
 */
public class EventSort {
    public static void main(String[] args){
        Integer[] arr = {5,5,2,6,3,4};
        for (Integer integer : arr) {
            System.out.print(integer+" ");
        }
        //flag記錄當前迭代是否發生了數據交換
        //start表示是奇交換仍是偶交換 0表示偶交換
        int flag = 1,start = 0;
        //若是上一次比較發生了數據交換,或當前正在進行的是奇交換,循環就不會中止,知道程序再也不發生交換
        //而且當前進行的是偶交換爲止(表示奇偶交換已經成對出現)
        while (flag==1||start==1){
            flag = 0;
            for (int i = start; i < arr.length-1; i+=2) {
                if (arr[i]>arr[i+1]){
                    int temp = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = temp;
                    flag = 1;
                    System.out.println();
                    for (Integer integer : arr) {
                        System.out.print(integer+" ");
                    }
                }
            }
            //每次迭代結束切換start狀態
            if (start == 0)
                start = 1;
            else
                start = 0;
        }
        System.out.println("");
        for (Integer integer : arr) {
            System.out.print(integer+" ");
        }
    }
    //5 5 2 6 3 4    //55  26  34
    //5 2 5 6 3 4    // <-  5  52   63   4
    //5 2 5 3 6 4
    //2 5 5 3 6 4
    //2 5 3 5 6 4
    //2 5 3 5 4 6
    //2 3 5 5 4 6
    //2 3 5 4 5 6
    //2 3 4 5 5 6
    //2 3 4 5 5 6
}
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 奇偶交換排序 並行模式
 */
public class EventSort2 {
    static Integer[] arr = {5,5,2,6,3,4};
    static int flag = 1;
    static ExecutorService pool = Executors.newCachedThreadPool();
    static synchronized void setFlag(int v){
        flag = v;
    }

    static synchronized int getFlag() {
        return flag;
    }

    public static class OddEventSortTask implements Runnable{
        int i;
        CountDownLatch latch;
        public OddEventSortTask(int i, CountDownLatch latch) {
            this.i = i;
            this.latch = latch;
        }
        @Override
        public void run() {
            if (arr[i]>arr[i+1]) {
                int temp = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = temp;
                setFlag(1);
                System.out.println();
                for (Integer integer : arr) {
                    System.out.print(integer+" ");
                }
            }
            latch.countDown();
        }
    }
    public static void pOddEventSort() throws InterruptedException{
        int start = 0;
        while (getFlag()==1 || start == 1){
            setFlag(0);
            //數組比較的對數
            // start = 0;
            // 12 34 56   ->3   -> 6/2-0
            // 12 34 56 7  -> 3  ->7/2-0
            // start = 1;
            // 1 23 45 6   ->2   -> 6/2-1
            // 1 23 45 67  -> 3 -> 7/2-0
            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;
        }
        Thread.sleep(100);
        pool.shutdown();
    }
    public static void main(String[] args) throws InterruptedException {
        pOddEventSort();
        System.out.println();
        for (Integer integer : arr) {
            System.out.print(integer+" ");
        }
    }
    //5 2 5 3 6 4
    //5 2 5 3 6 4
    //2 5 5 3 6 4
    //2 5 3 5 6 4
    //2 5 3 5 4 6
    //2 5 3 4 5 6
    //2 3 5 4 5 6
    //2 3 4 5 5 6
    //2 3 4 5 5 6
}
相關文章
相關標籤/搜索