Algs4-2.2.28自顶向下与自底向上

 2.2.28自顶向下与自底向上。对于N=10^3、10^4、10^5和10^6,使用SortCompare比较自顶向下和自底向上的归并排序的性能。

图片
import java.util.Arrays;
public class E2d2d28
{
  
    public static double time (String alg,Double[] a)
    {
        Stopwatch timer =new Stopwatch();
        if(alg.equals("UB")) Merge.sort(a);
        if(alg.equals("BU")) MergeBU.sort(a);
        return timer.elapsedTime();
    }
   
    public static double timeRandomInput(String alg,int N,int T)
    {
        double total =0.0;
        Double[] a=new Double[N];
        for (int t=0;t<T;t++)
        {
            for (int i=0;i<N;i++)
                a[i]=StdRandom.uniform();
            total+=time(alg,a);
        }
        return total/T;
    }//end timeRandomInput


   
    public static void main(String[] args)
    {
        int N=Integer.parseInt(args[0]);
        int T=Integer.parseInt(args[1]);
        StdOut.printf("For %d random Doubles %d times sort\n",N,T);

        double t1=timeRandomInput("UB",N,T);
        double t2=timeRandomInput("BU",N,T);
        StdOut.printf("UB sort spend time=%.2f, BU=%.2f,BU/UB=%.2f \n",t1,t2,t2/t1);
    }
}


import java.util.Arrays;
public class Merge
{
    private static Comparable[] aux;
    public static void sort(Comparable[] a)
    {
        aux=new Comparable[a.length];
        sort(a,0,a.length-1);
    }
    public static void sort(Comparable[] a,int lo,int hi)
    {
        if (hi<=lo) return;
        int mid=lo+(hi-lo)/2;
        sort(a,lo,mid);
        sort(a,mid+1,hi);
        merge(a,lo,mid,hi);
    }
   
    public static void merge(Comparable[] a,int lo,int mid,int hi)
    {
        int i=lo,j=mid+1;
        for (int k=lo;k<=hi;k++)
        aux[k]=a[k];
       
        for(int k=lo;k<=hi;k++)
        if        (i>mid) a[k]=aux[j++];
        else if (j>hi)    a[k]=aux[i++];
        else if (less(aux[j],aux[i])) a[k]=aux[j++];
        else            a[k]=aux[i++];
      }
    private static boolean less(Comparable v,Comparable w)
    { return v.compareTo(w)<0;}

     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 N=Integer.parseInt(args[0]);
       Comparable[] a=new Comparable[N];
       for(int i=0;i<N;i++)
          a[i]=StdRandom.uniform();
        sort(a);
        StdOut.printf("isSorted=%s",isSorted(a));
    }
}


public class MergeBU
{
    private static Comparable[] aux;
    public static void sort(Comparable[] a)
    {
        int N=a.length;
        aux=new Comparable[N];
        for(int sz=1;sz<N;sz=sz+sz)
            for(int lo=0;lo<N-sz;lo+=sz+sz)
               merge(a,lo,lo+sz-1,Math.min(lo+sz+sz-1,N-1));
    }
   
    public static void merge(Comparable[] a,int lo,int mid,int hi)
    {
        int i=lo,j=mid+1;
        for (int k=lo;k<=hi;k++)
           aux[k]=a[k];
       
        for(int k=lo;k<=hi;k++)
        if        (i>mid) a[k]=aux[j++];
        else if (j>hi)    a[k]=aux[i++];
        else if (less(aux[j],aux[i])) a[k]=aux[j++];
        else            a[k]=aux[i++];
      }
   
    private static boolean less(Comparable v,Comparable w)
    { return v.compareTo(w)<0;}
         
      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 N=Integer.parseInt(args[0]);
       Comparable[] a=new Comparable[N];
       for(int i=0;i<N;i++)
          a[i]=StdRandom.uniform();
        sort(a);
        StdOut.printf("isSorted=%s",isSorted(a));
    }
}


posted @ 2018-10-27 09:32  修电脑的龙生  阅读(143)  评论(0编辑  收藏  举报