Java併發編程 希爾排序

1、插入排序

插入排序是一種很常用的排序算法。它的基本思想是:一個未排序的數據分成兩部分,前半部是已經排序的,後半部分是未排序的。在進行排序時,只需要在未排序的部分中選擇一個元素,將其插入到前面有序的數組中即可。最終未排序的部分會越來越少,直到爲0,那麼排序就完成了。

平均時間複雜度 O(n2) 最好情況是 O(n) 最差時間是O(n2)
代碼如下:


public class InsertSortTest {

    public static void main(String args[]) {

        int a[] = { 1, 5, 3, 6, 34, 45, 2, 54, 415 };
        insertSort(a);
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
        }

    }

    public  static void insertSort(int a[]) {
        int len = a.length;
        int i, j, key;

        for (i = 1; i < len; i++) {
            j = i - 1;
            key = a[i];
            while (j >= 0 && key < a[j]) {
                a[j + 1] = a[j];
                j--;
            }
            a[j + 1] = key;
        }

    }

}

2、希爾排序

希爾排序是對插入排序的擴展,它將整個數組根據間隔h分割爲若干個子數組。子數組相互穿插在一起,每一次排序時,分別對每一個子數組進行排序。
希爾排序的一個主要優點是,即使一個較小的元素在數組的末尾,由於每次元素移動都以h爲間隔進行,因此數組末尾的小元素可以在很少的交換次數下,就被置換到最接近元素最終的位置的地方。
平均時間複雜度 O(n1.3) 最好情況是 O(n) 最差時間是O(n2)

public static void main(String args[]) {
        int a[] = { 1, 5, 3, 6, 34, 45, 2, 54, 415 };
        shellSort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }

    }

    public static void shellSort(int a[]) {
        int len = a.length;
        int h = 1;
        while (h <= len / 3) {
            h = h * 3 + 1;
        }

        while (true) {

            for (int n = 0; n < h; n++) {

                int i, j, key;
                for (i = n+h; i < len; i =i+h) {
                    if (a[i] < a[i - h]) {
                        j = i - h;
                        key = a[i];
                        while (j >= 0 && a[j] > key) {
                            a[j + h] = a[j];
                            j -= h;

                        }
                        a[j + h] = key;
                    }

                }
            }

            if(h==1){
                break;
            }
            h = (h - 1) / 3;
        }

    }

}

3、並行改進希爾排序

在子數組之間進行的插入排序是互相不會干擾,因此可以並行進行。

public class ParllShellSortTest {
    static int[] a = { 1, 4, 2, 5, 6, 3 };
    static ExecutorService pool = Executors.newCachedThreadPool();

    public static class ShellSortTask implements Runnable {

        int x = 0;
        int h = 0;
        CountDownLatch l;

        public ShellSortTask(int x, int h, CountDownLatch latch) {
            this.x = x;
            this.h = h;
            this.l = latch;

        }

        public void run() {

            int i, j, key;
            for (i = x + h; i < a.length; i = i + h) {
                if (a[i] < a[i - h]) {
                    j = i - h;
                    key = a[i];
                    while (j >= 0 && a[j] > key) {
                        a[j + h] = a[j];
                        j -= h;

                    }
                    a[j + h] = key;
                }

            }

            l.countDown();

        }
    }

    public static void pShellSort(int[] arr) throws InterruptedException {
        // 計算出最大的n值
        int h = 1;
        CountDownLatch lathc = null;
        while (h <= arr.length / 3) {
            h = h * 3 + 1;
        }
        while (h > 0) {
            System.out.println("h=" + h);

            lathc = new CountDownLatch(h);
            for (int x = 0; x < h; x++) {
                pool.submit(new ShellSortTask(x, h, lathc));
            }
            lathc.await();
            // 計算下一個h值
            h = (h - 1) / 3;
        }
        pool.shutdown();
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println(Arrays.toString(a));
        pShellSort(a);
        for (int i : a) {
            System.out.print(i);
        }
        System.out.println("end");
    }

}

PS:《實戰Java高併發程序設計》這本書寫的很好,但是第五章 中的希爾排序應該是寫錯了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章