zoukankan      html  css  js  c++  java
  • Algs4-2.3.27忽略小数组

    2.3.27忽略小数组。用实验对比以下处理小数组的方法和练习2.3.25的处理方法的效果:在快速排序中直接忽略小数组,仅在快速排序结束后运行一次插入排序。注意:可以通过这些实验估计出电脑的缓存大小,因为当数组大小超出缓存时这种方法的性能可能会下降。
    public class E2d3d27
    {
        public static void sort1(Comparable[] a,int M)
        {
          //StdRandom.shuffle(a);
          sort1(a,0,a.length-1,M);
        }
       
        private static void sort1(Comparable[] a,int lo,int hi,int M)
        {
            //数组少于等于M个元素时使用插入排序
            if (hi-lo+1<M)
            {
                InsertSort(a,lo,hi);
                return;
            }
            int j=partition(a,lo,hi);
            sort1(a,lo,j-1,M);
            sort1(a,j+1,hi,M);
        }
     
        public static void sort2(Comparable[] a,int M)
        {
          //StdRandom.shuffle(a);
          sort2(a,0,a.length-1,M);
        }
       
        private static void sort2(Comparable[] a,int lo,int hi,int M)
        {
            //数组少于等于M个元素时使用插入排序
            if (hi-lo+1<M) return;
            int j=partition(a,lo,hi);
            sort2(a,lo,j-1,M);
            sort2(a,j+1,hi,M);
        }
       
        private static int partition(Comparable[] a,int lo,int hi)
        {
            int i=lo,j=hi+1;
            Comparable v=a[lo];
            while(true)
            {
                while(less(a[++i],v)) if(i==hi) break;
                while(less(v,a[--j])) if(j==lo) break;
              
                if(i>=j) break;
                exch(a,i,j);
            }
            exch(a,lo,j);
            return j;
        }
       

         private static void InsertSort(Comparable[] a,int lo,int hi)
        {
            for (int i=lo+1;i<hi+1;i++)
            {
                for (int j=i;j>0 && less(a[j],a[j-1]);j--)
                    exch(a,j,j-1);
              }
        }
           
        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=1;i<a.length;i++)
                if(less(a[i],a[i-1])) return false;
            return true;
        }
     
     
        public static void main(String[] args)
        {
            int Nlen[]={100000,500000,1000000,5000000};
            for(int Ni=0;Ni<Nlen.length;Ni++)
            {
              int N=Nlen[Ni];
              //生成一个长度为N的数组a
              Double[] a=new Double[N];
              for(int i=0;i<N;i++)
                a[i]=StdRandom.random(); 
              //
              int M=11;
              //生成数组a的两个副本aClone1、aClone1
              Double[] aClone1=new Double[N];
              for (int i=0;i<a.length;i++)
                  aClone1[i]=a[i];
             
              Double[] aClone2=new Double[N];
              for (int i=0;i<a.length;i++)
                 aClone2[i]=a[i];
              //数组长度为N,小于等于M时切换到插入排序的快速排序
              Stopwatch timer1=new Stopwatch();
              sort1(aClone1,M);
              double t1=timer1.elapsedTime();
              //数组长度为N,小于等于M忽略小数组的快速排序
              Stopwatch timer2=new Stopwatch();
              sort2(aClone2,M);
              InsertSort(aClone2,0,aClone2.length-1);
              double t2=timer2.elapsedTime();
             
              StdOut.printf("N=%d,T1=%f,T2=%f,t1-t2=%f ",N,t1,t2,t1-t2);
            }//end for Ni
          
        }//end main
    }//end class

  • 相关阅读:
    Codeforces Round #603 (Div. 2)
    【bzoj1997】[Hnoi2010]Planar(平面图+2-sat)
    【poj3207】Ikki's Story IV
    【HDU1814】Peaceful Commission(2-sat+暴力染色)
    Educational Codeforces Round 77 (Rated for Div. 2)
    【hdu3311】Dig The Wells(斯坦纳树+dp)
    [USACO3.3] A Game
    [TJOI2013] 单词
    [USACO3.3] Home on the Range
    [NOI2011] 阿狸的打字机
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9868585.html
Copyright © 2011-2022 走看看