// IE Test: See SortAnimate.java
import java.applet.Applet;
import java.awt.*;
abstract public class Animate 
  extends Applet implements Runnable 
  { Graphics g;
    Thread animatorThread;
    int dotSize, inGen;
    int N; double[] a;
    public void start() 
      { g = getGraphics();
        new Thread(this).start(); 
      }
    public void stop() { animatorThread = null; }
    public void run() 
      { N = Integer.parseInt(getParameter("N"));  
        dotSize = Integer.parseInt(getParameter("dotSize"));  
        inGen = Integer.parseInt(getParameter("inGen"));  
        g.setColor(Color.white); 
        for (int i = 0; i < 20; i++)
          g.fillRect(0, 0, getSize().width, getSize().height); 
        a = new double[N];
        if (inGen == 1)
          for (int i = 0; i < N; i++) a[i] = Math.random();
        if (inGen == 2)
          for (int i = 0; i < N; i++) a[i] = (N-1-i)/(double) N;
        if (inGen == 4)
          for (int i = 0; i < N; i++) 
	    if ((i % 2) == 0)
                 a[i] = ((int) (3*Math.random()))/3.0;
            else a[i] = Math.random();
        for (int i = 0; i < N; i++) 
          dot(X(i), Y(a[i]), Color.black);
        sort(a, 0, N-1);
      }
    int X(int i) 
      { return ((i+1)*getSize().width)/(N+2); }
    int Y(double v) 
      { return (int) (0.005 + (0.99 - 0.99*v)*getSize().height); }
    void dot(int x, int y, Color c) 
      { g.setColor(c); g.fillOval(x, y, dotSize, dotSize); }
    boolean less(double [] a, int i, int j) 
      { 
        dot(X(i), Y(a[i]), Color.black);
        dot(X(j), Y(a[j]), Color.black);
        return a[i] < a[j]; 
      }
    void exch(double [] a, int i, int j) 
      { double t = a[i]; a[i] = a[j]; a[j] = t;
        dot(X(i), Y(a[j]), Color.green);
        dot(X(j), Y(a[i]), Color.green);
        dot(X(i), Y(a[i]), Color.black);
        dot(X(j), Y(a[j]), Color.black);
      }
    abstract void sort(double a[], int l, int r); 
}

