• 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;
        }
       
      
    }

  • 相关阅读:
    codeforces
    codeforces
    HDU
    poj
    poj
    HDU Problem
    HDU 3555 Bomb 【数位dp】
    POJ 1942 Paths on a Grid【组合数学】
    杭电 Problem 2089 不要62 【数位dp】
    codeforces 486c-Palindrome Transformation【贪心】
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9860018.html
Copyright © 2020-2023  润新知