zoukankan      html  css  js  c++  java
  • Algs4-2.1.11希尔排序序列改为存数组

    2.1.11将希尔排序中实时计算递增序列改为预先计算并存储在一个数组中。
    public class Shell2
    {
        public static void sort(Comparable[] a)
        {
            int N=a.length;
            int h=1;
            int[] SN=new int[20];
            int hMaxIndex=0;
            while (h<N/3)
            {
               SN[hMaxIndex]=h;
                h=3*h+1;
                hMaxIndex++;
            }

           
            for (int hIndex=hMaxIndex;hIndex>0;hMaxIndex--)
            {
                h=SN[hIndex];
                for (int i=h;i<N;i++)
                {
                    for (int j=i;j>=h && less(a[j],a[j-h]);j-=h)
                        exch(a,j,j-h);
                }
            }
        }
       
        private static boolean less(Comparable v,Comparable w)
        { return v.compareTo(w)<0;}
       
        private static void exch(Comparable[] a,int i,int j)
        {
            Comparable t=a[i];
            a[i]=a[j];
            a[j]=t;
        }
       
        private static void show(Comparable[] a)
        {
            for (int i=0;i<a.length;i++)
                StdOut.print(a[i]+" ");
            StdOut.println();
        }
       
        public static boolean isSorted(Comparable[] a)
        {
            for (int i=0;i<a.length;i++)
                if(less(a[i],a[i-1])) return false;
            return true;
        }
       
      
    }

  • 相关阅读:
    print 参数
    note
    action标签的属性说明
    Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver'
    润乾报表
    javax.naming.NameNotFoundException: Name ZKING is not bound in this Context 的问题
    Myeclipse2013安装svn插件
    Myeclipse2013的优化设置
    Myeclipse解析.classpath文件
    Struts
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9860018.html
Copyright © 2011-2022 走看看