Demo entry 6661173

as

   

Submitted by anonymous on Nov 20, 2017 at 13:11
Language: Java. Code size: 5.0 kB.

public class slow
{
  public static void sort(int[] a)
  {
    int N = a.length;
    for(int i=1; i<N; i++)
    {
      for(int j=i; j>0 && a[j] < a[j-1]; j--)
      {
        int temp = a[j] ;
        a[j] = a[j-1];
        a[j-1] = temp;
      }
    }
  }
}

public class IntegerTest
{
  private static final int MAXIMUM_INTEGER = 1000000;
  
  private IntegerTest(){ }
  public static double timeTrial(int n)
  {
   int[] a = new int[n];
   for(int i = 0;i < n; i++)
   {
     a[i] = StdRandom.uniform(-MAXIMUM_INTEGER,MAXIMUM_INTEGER);
   }
   Stopwatch timer = new Stopwatch();
   slow.sort(a);// countinue....
   return timer.elapsedTime();
  }
  
  public static void main(String args[])
  {
    int cnt=0;
    double[] aux = new double[11];
    for(int n = 200; n<=204800; n*=2)
    {
      
      double time = timeTrial(n);
      aux[cnt] = time;
      cnt++;
      StdOut.printf("%7d %7.3f\n", n, time);    
    }
    
    
    DrawPlot.Draw();
    double x=0;
    for(int j=0;j<10;j++)
    {
      DrawPlot.draw(x,aux[j],x+0.1,aux[j+1]);
      x+=0.1;
    }
  }
}

public class Merge
{
  private static int[] aux;
  
  public static void merge(int[] 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(aux[j] < aux[i]) a[k] = aux[j++];
      else                     a[k] = aux[i++];    
  }
  public static void sort (int[] a)
  {
    aux = new int[a.length];
    sort(a, 0, a.length-1);
  }
  
  private static void sort(int[] 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 class MergeTest
{
  private static final int MAXIMUM_INTEGER = 1000000;
  
  private MergeTest(){ }
  public static double timeTrial(int n)
  {
   int[] a = new int[n];
   for(int i = 0;i < n; i++)
   {
     a[i] = StdRandom.uniform(-MAXIMUM_INTEGER,MAXIMUM_INTEGER);
   }
   Stopwatch timer = new Stopwatch();
   Merge.sort(a);
   return timer.elapsedTime();
  }
  
  public static void main(String args[])
  {
    int cnt=0;
    double[] aux = new double[11];
    for(int n = 200; n<=204800; n*=2)
    {
      double time = timeTrial(n);
      aux[cnt] = time;
      cnt++;
      StdOut.printf("%7d %7.3f\n", n, time);
    }
    
    DrawPlot.Draw2();
    double x=0;
    for(int j=0;j<10;j++)
    {
      DrawPlot.draw2(x,aux[j],x+0.1,aux[j+1]);
      x+=0.1;
    }
  }
}



public class DrawPlot
{
       public static void Draw() 
       { 
           StdDraw.setCanvasSize(700,700);
           StdDraw.setPenRadius(0.01);
           StdDraw.setPenColor(StdDraw.BLACK);
           StdDraw.setXscale();
           StdDraw.setPenColor(StdDraw.BLACK);
           StdDraw.setYscale();
           //StdDraw.line(0.1, 0.2, 1.02, 0.2);
           StdDraw.line(0, 0.2, 1.0, 0.2);
            StdDraw.setPenColor(StdDraw.BLACK);
           double a = 0;
            for(int i=200; i<=204800; i*=2)
            {
              StdDraw.text(a,0.15,Integer.toString(i));
              a+=0.1;
            }
            //StdDraw.line(0.1, 0.2, 0.1, 1.02);
            StdDraw.line(0, 0.2, 0, 1.);
            double b = 0; 
            for(int i=0; i<10; i++)
            {
              StdDraw.text(-0.03,b+0.2,Integer.toString(i));
              b+=0.1;
            }
       }
       public static void Draw2() 
       { 
           StdDraw.setCanvasSize(700,700);
           StdDraw.setPenRadius(0.01);
           StdDraw.setPenColor(StdDraw.BLACK);
           StdDraw.setXscale();
           StdDraw.setPenColor(StdDraw.BLACK);
           StdDraw.setYscale();
           //StdDraw.line(0.1, 0.2, 1.02, 0.2);
           StdDraw.line(0, 0.2, 1.0, 0.2);
            StdDraw.setPenColor(StdDraw.BLACK);
           double a = 0;
            for(int i=200; i<=204800; i*=2)
            {
              StdDraw.text(a,0.15,Integer.toString(i));
              a+=0.1;
            }
            //StdDraw.line(0.1, 0.2, 0.1, 1.02);
            StdDraw.line(0, 0.2, 0, 1.);
            double b = 0; 
            for(int i=0; i<10; i++)
            {
              StdDraw.text(-0.03,b+0.2,Double.toString(i*0.01));
              b+=0.1;
            }
       }
       
       public static void draw(double x1,double y1,double x2,double y2)
       {
         StdDraw.setPenRadius(0.009);
         StdDraw.setPenColor(StdDraw.GRAY);
         StdDraw.line(x1,y1*0.125+0.2,x2,y2*0.125+0.2);
       }
       public static void draw2(double x1,double y1,double x2,double y2)
       {
         StdDraw.setPenRadius(0.009);
         StdDraw.setPenColor(StdDraw.GRAY);
         StdDraw.line(x1,y1*10+0.2,x2,y2*10+0.2);
       }    
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).