Algorithm (D) shell sort

Sort principle

Shell sort is the main element in the array according to a certain step length packets, like elements are divided into a set of steps, shuffling is a logical grouping, where the array elements does not change the position of the later packets in accordance with insertion sort for each sorting group, each group when the sorting is completed, the step size is decremented when the step size to 1 in descending order, the array is already sorted up.

time complexity

Shell sort is the sort of instability, when after a lot of experiments, the average time complexity of Shell sort is O (n1.3).

Sort Code

package com.alg.sort;

public class ShellSort {
    /**
     * 希尔排序算法:从插入排序延伸过来的
     */
    public void shellSort(int[] arr, int a, int n) {
        /**
         * 设置增量步长为数组的一半,该排序算法最复杂的就是该步长的确定
         */
        for (int i = arr.length / 2; i > 0; i /= 2) {
            /**
             * 确定步长的两个元素
             * 以10为例,步长为5,那么第一次循环的元素的下标为j=5,j++一直遍历到数组的尾部
             */
            for (int j = i; j < arr.length; j++) {
                //将下标为5的元素复制一份,该元素是分组中最右边的元素
                int e = arr[j];
                //记录该元素需要放置的位置。
                int index;

                /**
                 *分组,以步长5为距离,将元素逻辑上分组,改分组是逻辑上的,元素的位置 本身不变的。
                 * 原理:步长5为距离的分组中最右边的元素减去步长,那么该元素就是该分组中
                 * 最左边的元素。这样我们来计算数组最右边的元素是否小于最左边的元素,如果为true,
                 * 就将最左的元素复制给最右边的元素,然后把事先复制出来的最右的元素复制到最左边的元素,
                 * 请注意:当循环结束的时候,index由于执行了 index-=i了,这个时候index就成负数,只有
                 * 再次将步长加回来,才是真正最左边的下标位置。
                 */
                for (index = j - i; index >= 0 && arr[index] > e; index -= i) {
                    arr[index + i] = arr[index];
                }
                arr[index + i] = e;
            }
        }
    }
}

Published 94 original articles · won praise 55 · views 110 000 +

Guess you like

Origin blog.csdn.net/Suubyy/article/details/100100335