• 希尔排序


    希尔排序是基于插入排序的以下性质而提出改进方法的:
    1. 插入排序在对几乎已经排好序的数据操作时,效率高,即可以达到线性排序的效率。
    2. 但插入排序一般来说是低效的,因为插入排序每次只能将数据移动一位,因此插入排序只能将元素一点一点地从数组的一端移动到另一端。
    3. 希尔排序没有减少元素的比较次数,但减少了元素的移动次数。
     1 public class Shell extends Sort {
     2     /**
     3      * 将a[]升序排列
     4      * @param a
     5      * @return
     6      */
     7     public static Comparable[] shell(Comparable[] a) {
     8         int N = a.length;
     9         int h = 1;
    10         // h以3为底数:h={1,4,13,40...}
    11         while (h < N / 3) {
    12             h = 3 * h + 1;
    13         }
    14         while (h >= 1) {
    15             for (int i = h; i < N; i++) {
    16                 // 从j-h=0号开始,以增幅h开始排序。分组排序
    17                 for (int j = i; j >= h && less(a[j], a[j - h]); j -= h) {
    18                     exch(a, j, j - h);
    19                 }
    20             }
    21             // h值为希尔排序增量,最后值必须为1,h={...40,13,4,1}
    22             h = h / 3;
    23         }
    24         return a;
    25     }
    26 
    27     public static void main(String[] args) {
    28         // TODO Auto-generated method stub
    29         String[] strings = { "S", "H", "E", "L", "Z", "L", "S", "O", "R", "T",
    30                 "A", "Q", "B" };
    31         String[] strings2 = (String[]) shell(strings);
    32         for (String string : strings2) {
    33             System.out.print(string + "   ");
    34         }
    35     }
    36 
    37 }

    父类Sort:

     1 public  class Sort {
     2     protected static boolean less(Comparable a, Comparable b) {
     3         return a.compareTo(b) < 0;
     4     }
     5 
     6     protected static void exch(Comparable[] a, int i, int j) {
     7         Comparable tComparable = a[i];
     8         a[i] = a[j];
     9         a[j] = tComparable;
    10     }
    11 
    12     public  void show(Comparable[] a) {
    13         for (Comparable comparable : a) {
    14             StdOut.print(comparable + "  ");
    15         }
    16         StdOut.println();
    17     }
    18 
    19     protected static boolean isSorted(Comparable[] a) {
    20         for (int i = 1; i < a.length; i++) {
    21             if (less(a[i], a[i - 1])) {
    22                 return false;
    23             }
    24         }
    25         return true;
    26     }
    27 }
  • 相关阅读:
    二分查找
    苹果开发人员账号注冊流程
    cocos2d_android 瞬间动作
    Qt多线程学习:创建多线程
    Java模式(适配器模式)
    代理方法keywordAction与Fun的使用
    装饰者模式
    编写你自己的单点登录(SSO)服务
    4种Java引用浅解
    strtok和strtok_r
  • 原文地址:https://www.cnblogs.com/mada0/p/4695717.html
Copyright © 2020-2023  润新知