Algs4-2.1.36不均匀的数据

2.1.36不均匀的数据。编写一个测试用例,生成不均匀的测试数据,包括:
1)一半数据是0,一半是1;
2)一半数据是0,1/4是1,1/4是2,以此类推;
3)一半数据是0,一半是随机int值。
评估并验证这些输入数据对本节讨论的算法的性能的影响。
1)打乱次序后的结果:
一百万个数10次运行的平均时间。选择排序性能最差。希尔排序性能最好。
图片

2)未打乱次序后的结果:
一百万个数10次运行的平均时间。选择排序性能最差。希尔排序整体性能最好,对于数据情况1,2
时希尔排序的性能比插入排序优势不明显。
图片
以下是未打乱的代码,打乱的代码只需去掉打代码的注释即可。
public class E2d1d36
{
    public static double time (String alg,Integer[] a)
    {
        Stopwatch timer =new Stopwatch();
        if(alg.equals("Insertion")) Insertion.sort(a);
        if(alg.equals("Selection")) Selection.sort(a);
        if(alg.equals("Shell")) Shell.sort(a);
      // if(alg.equals("Merge")) Merge.sort(a);
      //  if(alg.equals("Quick")) Quick.sort(a);
      //  if(alg.equals("Heap")) Heap.sort(a);
        return timer.elapsedTime();
    }
   
    public static double timeRandomInput(String alg,int N,int T,int probabilityTYPE)
    {
        double total =0.0;
        Integer[] a=new Integer[N];

        if (probabilityTYPE==0)
        {
            for (int t=0;t<T;t++)
            {
                generateHalf0Half1(a);
                total+=time(alg,a);
            }
        }
        //probabilityTYPE=1 Poisson distribution
        if (probabilityTYPE==1)
        {
            for (int t=0;t<T;t++)
            {
                generateHalf012(a);
                total+=time(alg,a);
            }
        }
        //probabilityTYPE=2 Geometric distribution
        if (probabilityTYPE==2)
        {
            for (int t=0;t<T;t++)
            {
                generateHalf0HalfRandom(a);
                total+=time(alg,a);
            }
        }
       return total;
    }//end timeRandomInput

    public static void generateHalf0Half1(Integer[] a)
    {
        int half=a.length/2;
        for (int i=0;i<half;i++)
                    a[i]=0;
        for (int i=half;i<a.length;i++)
                    a[i]=1;
        //StdRandom.shuffle(a);
    }
   
     public static void generateHalf012(Integer[] a)
    {
         int half=a.length/2;
         int over14=a.length/4;
        for (int i=0;i<half;i++)
                    a[i]=0;
        for (int i=half;i<half+over14;i++)
                    a[i]=1;
        for (int i=half+over14;i<a.length;i++)
                    a[i]=2;
        //StdRandom.shuffle(a);
    }
    
     public static void generateHalf0HalfRandom(Integer[] a)
    {
        int half=a.length/2;
        for (int i=0;i<half;i++)
                    a[i]=0;
        for (int i=half;i<a.length;i++)
                    a[i]=StdRandom.uniform(10*a.length);
        //StdRandom.shuffle(a);
    }
   
    public static void main(String[] args)
    {
     
        Integer N=Integer.parseInt(args[0]);
        Integer T=Integer.parseInt(args[1]);
               
        String[] CASEmem=new String[4];
        CASEmem[0]="1/2 0,1/2 1";
        CASEmem[1]="1/2 0,1/4 1,1/4 2";
        CASEmem[2]="1/2 0,1/2 random";


         Double time;
         StdOut.printf("---Insertion---\n");
          time=timeRandomInput("Insertion",N,T,0);
         StdOut.printf("For %d  int with case %s,spend time=%.2f\n",N,CASEmem[0],time);
         time =timeRandomInput("Insertion",N,T,1);
         StdOut.printf("For %d  int with case %s,spend time=%.2f\n",N,CASEmem[1],time);
         time =timeRandomInput("Insertion",N,T,2);
         StdOut.printf("For %d  int with case %s,spend time=%.2f\n",N,CASEmem[2],time);


         StdOut.printf("---Selection---\n");
         time =timeRandomInput("Selection",N,T,0);
         StdOut.printf("For %d  int with case %s,spend time=%.2f\n",N,CASEmem[0],time);
         time =timeRandomInput("Selection",N,T,1);
         StdOut.printf("For %d  int with case %s,spend time=%.2f\n",N,CASEmem[1],time);
         time =timeRandomInput("Selection",N,T,2);
         StdOut.printf("For %d  int with case %s,spend time=%.2f\n",N,CASEmem[2],time);

        
         StdOut.printf("---Shell---\n");
         time =timeRandomInput("Shell",N,T,0);
         StdOut.printf("For %d  intuble with case %s,spend time=%.2f\n",N,CASEmem[0],time);
         time =timeRandomInput("Shell",N,T,1);
         StdOut.printf("For %d  int with case %s,spend time=%.2f\n",N,CASEmem[1],time);
         time =timeRandomInput("Shell",N,T,2);
         StdOut.printf("For %d  int with case %s,spend time=%.2f\n",N,CASEmem[2],time);

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