zoukankan      html  css  js  c++  java
  • 插入排序->希尔排序

    
    /**
     * 插入排序
     */
    public class InsertSort {
        public static void main(String[] args){
            int[] arr = {5,5,2,6,3,4};
            int length = arr.length;
            int a,b,key;
            for (a = 1; a < length; a++) {
                key = arr[a];
                b = a - 1; //1
                while (b>=0&&arr[b]>key){
                    arr[b+1] = arr[b];
                    b--;
                }
                //while不执行arr[b+1]->arr[a]
                arr[b+1] = key;
                System.out.println();
                for (Integer integer : arr) {
                    System.out.print(integer+" ");
                }
            }
            //5 5 2 6 3 4
            //2 5 5 6 3 4
            //2 5 5 6 3 4
            //2 3 5 5 6 4
            //2 3 4 5 5 6
        }
    }
    
    
    /**
     * 希尔排序
     */
    public class ShellSort1 {
        public static void main(String[] args) {
            int[] arr = {5, 5, 2, 6, 3, 4};
            for (Integer integer : arr) {
                System.out.print(integer+" ");
            }
            int h = 1;
            while (h <= arr.length / 3) {
                h = h * 3 + 1;
            }
            System.out.println();
            System.out.println("h:"+h);
            while (h > 0) {
                for (int a = h; a < arr.length; a++) {
                    if (arr[a] < arr[a - h]) {
                        int tmp = arr[a];
                        int b = a - h;
                        while (b >= 0 && arr[b] > tmp) {
                            arr[b + h] = arr[b];
                            b -= h;
                        }
                        arr[b + h] = tmp;
                        System.out.println();
                        for (Integer integer : arr) {
                            System.out.print(integer+" ");
                        }
                    }
                }
                h = (h - 1) / 3;
                System.out.println();
                System.out.println("h:"+h);
            }
            //5 5 2 6 3 4
            //h:4
            //
            //3 5 2 6 5 4
            //3 4 2 6 5 5
            //h:1
            //
            //2 3 4 6 5 5
            //2 3 4 5 6 5
            //2 3 4 5 5 6
            //h:0
        }
    }
    
    
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * 希尔排序 并行
     */
    public class ShellSort{
        static int[] arr = {5,5,2,6,3,4};
        static ExecutorService pool = Executors.newCachedThreadPool();
        public static class ShellSortTask implements Runnable {
            int i = 0, h = 0;
            CountDownLatch l;
            public ShellSortTask(int i, int h, CountDownLatch l) {
                this.i = i;
                this.h = h;
                this.l = l;
            }
    
            @Override
            public void run() {
                if (arr[i]<arr[i -h]){
                    int tmp = arr[i];
                    int b = i - h;
                    while (b>=0 && arr[b]>tmp){
                        arr[b+h] = arr[b];
                        b -= h;
                    }
                    arr[b+h] = tmp;
                    System.out.println();
                    for (Integer integer : arr) {
                        System.out.print(integer+" ");
                    }
                }
                l.countDown();
            }
        }
        public static void pShellSort() throws InterruptedException{
            int h = 1;
            CountDownLatch latch = null;
            while (h<=arr.length/3){
                h = h * 3 + 1;
            }
            while (h>0){
                System.out.println("h:"+h);
                if (h>=4){
                    latch = new CountDownLatch(arr.length-h);
                }
                for (int a = h; a < arr.length; a++) {
                    if (h >= 4) {
                        pool.execute(new ShellSortTask(a,h,latch));
                    } else {
                        if (arr[a] < arr[a - h]) {
                            int tmp = arr[a];
                            int b = a - h;
                            while (b >= 0 && arr[b] > tmp) {
                                arr[b + h] = arr[b];
                                b -= h;
                            }
                            arr[b + h] = tmp;
                            System.out.println();
                            for (Integer integer : arr) {
                                System.out.print(integer+" ");
                            }
                        }
                    }
                }
                latch.await();
                h = (h-1)/3;
            }
            pool.shutdown();
        }
        public static void main(String[] args) throws InterruptedException{
            pShellSort();
        }
        //h:4
        //
        //3 5 2 6 5
        //3 4 2 6 5 5 5 h:1
        //
        //2 3 4 6 5 5
        //2 3 4 5 6 5
        //2 3 4 5 5 6
    }
    
  • 相关阅读:
    php的命名空间层级与目录层级是一致的吗?
    PHP 反射应用之一(插件框架)
    一次http完整的请求tcp报文分析
    Restful based service 的跨域调用
    php 命名空间的目的
    浏览器跨域问题
    PHP 代码跟踪
    记一次分析别人源码的过程
    php script 的生命周期
    全局安装 vue
  • 原文地址:https://www.cnblogs.com/fly-book/p/11491280.html
Copyright © 2011-2022 走看看