奇偶交换排序


/**
 * 冒泡排序
 */
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
}
posted @ 2019-09-07 13:48  fly_bk  阅读(618)  评论(0编辑  收藏  举报