zoukankan      html  css  js  c++  java
  • 希尔排序

        /**
         * 希尔排序时, 对有序序列在插入时采用交换法
         * 时间复杂度O(n^(1.3-2))
         * @param arr
         */
        private static void shellSort(int[] arr) {
    
            int temp = 0;
            int count = 0;
            // 使用循环处理
            for (int gap = arr.length / 2; gap > 0; gap /= 2) {
                for (int i = gap; i < arr.length; i++) {
                    // 遍历各组中所有的元素(共gap组,每组有个元素), 步长gap
                    for (int j = i - gap; j >= 0; j -= gap) {
                        // 如果当前元素大于加上步长后的那个元素,说明交换
                        if (arr[j] > arr[j + gap]) {
                            temp = arr[j];
                            arr[j] = arr[j + gap];
                            arr[j + gap] = temp;
                        }
                    }
                }
            }
    
        }
    
        /**
         * 对交换式的希尔排序进行优化->移位法
         * @param arr
         */
        private static void shellSort2(int[] arr) {
    
            // 增量gap, 并逐步的缩小增量
            for (int gap = arr.length / 2; gap > 0; gap /= 2) {
                // 从第gap个元素,逐个对其所在的组进行直接插入排序
                for (int i = gap; i < arr.length; i++) {
                    int j = i;
                    int temp = arr[j];
                    if (arr[j] < arr[j - gap]) {
                        while (j - gap >= 0 && temp < arr[j - gap]) {
                            //移动
                            arr[j] = arr[j-gap];
                            j -= gap;
                        }
                        //当退出while后,就给temp找到插入的位置
                        arr[j] = temp;
                    }
    
                }
            }
        }
    
  • 相关阅读:
    post和get区别
    https
    tcp/ip协议
    webpack与gulp的不同
    什么是webpack
    spring boot 输入参数统一校验
    spring boot++jpa+ mysql +maven
    Intellij IDEA 2018.2.2 SpringBoot热启动 (Maven)
    git 从远程仓克隆到本地新分支
    ASP.NET MVC 自动模型验证
  • 原文地址:https://www.cnblogs.com/ding-dang/p/13391094.html
Copyright © 2011-2022 走看看