joe 发表于 2021-10-29 08:55:20

希尔排序

希尔排序(Shell Sort)是插入排序的一种,它是针对直接插入排序算法的改进。该方法又称缩小增量排序,因DL.Shell于1959年提出而得名。

希尔排序实质上是一种分组插入方法。它的基本思想是:对于n个待排序的数列,取一个小于n的整数gap(gap被称为步长)将待排序元素分成若干个组子序列,所有距离为gap的倍数的记录放在同一个组中;然后,对各组内的元素进行直接插入排序。 这一趟排序完成之后,每一个组的元素都是有序的。然后减小gap的值,并重复执行上述的分组和排序。重复这样的操作,当gap=1时,整个数列就是有序的。
————————————————
/**
* 希尔排序:Java
*
* @author skywang
* @date 2014/03/11
*/

public class ShellSort {

    /**
   * 希尔排序
   *
   * 参数说明:
   *   a -- 待排序的数组
   *   n -- 数组的长度
   */
    public static void shellSort1(int[] a, int n) {

      // gap为步长,每次减为原来的一半。
      for (int gap = n / 2; gap > 0; gap /= 2) {

            // 共gap个组,对每一组都执行直接插入排序
            for (int i = 0 ;i < gap; i++) {

                for (int j = i + gap; j < n; j += gap) {

                  // 如果a < a,则寻找a位置,并将后面数据的位置都后移。
                  if (a < a) {

                        int tmp = a;
                        int k = j - gap;
                        while (k >= 0 && a > tmp) {
                            a = a;
                            k -= gap;
                        }
                        a = tmp;
                  }
                }
            }
      }
    }

    /**
   * 对希尔排序中的单个组进行排序
   *
   * 参数说明:
   *   a -- 待排序的数组
   *   n -- 数组总的长度
   *   i -- 组的起始位置
   *   gap -- 组的步长
   *
   *组是"从i开始,将相隔gap长度的数都取出"所组成的!
   */
    public static void groupSort(int[] a, int n, int i,int gap) {

      for (int j = i + gap; j < n; j += gap) {

            // 如果a < a,则寻找a位置,并将后面数据的位置都后移。
            if (a < a) {

                int tmp = a;
                int k = j - gap;
                while (k >= 0 && a > tmp) {
                  a = a;
                  k -= gap;
                }
                a = tmp;
            }
      }
    }

    /**
   * 希尔排序
   *
   * 参数说明:
   *   a -- 待排序的数组
   *   n -- 数组的长度
   */
    public static void shellSort2(int[] a, int n) {
      // gap为步长,每次减为原来的一半。
      for (int gap = n / 2; gap > 0; gap /= 2) {
            // 共gap个组,对每一组都执行直接插入排序
            for (int i = 0 ;i < gap; i++)
                groupSort(a, n, i, gap);
      }
    }

    public static void main(String[] args) {
      int i;
      int a[] = {80,30,60,40,20,10,50,70};

      System.out.printf("before sort:");
      for (i=0; i<a.length; i++)
            System.out.printf("%d ", a);
      System.out.printf("\n");

      shellSort1(a, a.length);
      //shellSort2(a, a.length);

      System.out.printf("aftersort:");
      for (i=0; i<a.length; i++)
            System.out.printf("%d ", a);
      System.out.printf("\n");
    }
}
60 40 20 10 50 70
aftersort:10 20 30 40 50 60 70 80

帖子主体内容来自:原文链接:https://blog.csdn.net/qq_37592492/article/details/81157153
页: [1]
查看完整版本: 希尔排序