import java.awt.*;

/*
 * MergevsInsert.java
 *
 * Created on Octubre 05, 2001, 4:46 AM
 */


/**
 *
 * @author Oscar Medina Duarte 
 * @version
 */
public class MergevsInsert extends java.applet.Applet {
    
    private Label label1;
    private Label label2;
    private TextField textField1;
    private TextField textField2;
    private Button button1;
    private Button button2;
	private Button button3;
	private Button button4;
	private Button button5;

    private List list1;
    private List list2;
    private Label label3;
    private Label label4;
	private Label label5;
    private int array[],array1[];
   

    /** Initializes the applet InsertionSort */
    public void init () {
        initComponents ();
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    private void initComponents() {
        label1 = new Label();
        label2 = new Label();
        textField1 = new TextField("100");
        textField2 = new TextField("10000");
        button1 = new Button();
        button2 = new Button();
        button3 = new Button();
        button4 = new Button();
        button5 = new Button();

        list1 = new List();
        list2 = new List();
        label3 = new Label();
        label4 = new Label();
	label5 = new Label();
        setLayout(null);



        label1.setText("Tamano :");
        add(label1);
        label1.setBounds(0, 10, 70, 30);
        



        label2.setText("Cota Max :");
        add(label2);
        label2.setBounds(0, 40, 70, 30);
        
        

        textField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
               if (isNuma(evt)!=0){
                        textField1.setText("");
               }
            }
        }
        );
        
        add(textField1);
        textField1.setBounds(70, 10, 40, 30);
        
        

        textField2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                if (isNuma(evt)!=0){
                        textField2.setText("");
               }
            }
        }
        );
        
        add(textField2);
        textField2.setBounds(70, 40, 80, 30);
        
        

        button1.setLabel("Aleatorio");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                array=new int[((new Integer(textField1.getText())).intValue()+1)]; // Leer argumento de textField1
                array1=new int[((new Integer(textField1.getText())).intValue()+1)]; // Leer argumento de textField1
                
                Generate(evt);
		
		for (int u=0;u<array.length;u++)
			array1[u] = array[u];//.clone();
		
		//button2.setEnabled(true);
		//button5.setEnabled(true);
            }
        }
        );
        
        add(button1);
        button1.setBounds(110, 10, 70, 30);

	button3.setLabel("Ascendente");
        button3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                // Generar array y field de cero a etc...
		array=new int[((new Integer(textField1.getText())).intValue()+1)]; // Leer argumento de textField1
		list1.clear();
	        for (int g =1; g<array.length;g++){
 	               array[g]=g;
			list1.add(""+array[g]);
   		}
   		
   		for (int u=0;u<array.length;u++)
			array1[u] = array[u];//.clone();
   		
		//button2.setEnabled(true);
		//button5.setEnabled(true);

            }
        }
        );
        
        add(button3);
        button3.setBounds(185, 10, 80, 30);

	button4.setLabel("Desendente");
        button4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                // Generar array y field de cero a etc...
		array=new int[((new Integer(textField1.getText())).intValue()+1)]; // Leer argumento de textField1
		list1.clear();
	        for (int g =(array.length-1); 0<g;g--){
 	               array[g]=g;
			list1.add(""+array[g]);
   		}
   		
   		for (int u=0;u<array.length;u++)
			array1[u] = array[u];//.clone();
   		
		//button2.setEnabled(true);
		//button5.setEnabled(true);


            }
        }
        );
        
        add(button4);
        button4.setBounds(270, 10, 80, 30);

        
        
        button2.setLabel("InsertionSort");
        button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
		long time = System.currentTimeMillis();

                InsertionSort(array);
		label5.setText("Tiempo : "+(System.currentTimeMillis()-time)+" ms.");
		list2.clear();
	        for (int g =1; g<array.length;g++){
 	               list2.add(""+array[g]);
   		}
   		
   		for (int u=0;u<array.length;u++)
			array[u] = array1[u];//.clone();
   		
		//button2.setEnabled(false);
		//button5.setEnabled(false);


            }
        }
        );
       //button2.setEnabled(false); 
        add(button2);
        button2.setBounds(155 ,40, 95, 30);

	button5.setLabel("MergeSort");
        button5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                // Merge Sort
		long time = System.currentTimeMillis();
		
		MergeSort(array,0,array.length);
		
		label5.setText("Tiempo : "+(System.currentTimeMillis()-time)+" ms.");	
		list2.clear();
	        for (int g =1; g<array.length;g++){
 	               list2.add(""+array[g]);
   		}
   		
   		for (int u=0;u<array.length;u++)
			array[u] = array1[u];

		//button2.setEnabled(false);
		//button5.setEnabled(false);


            }
        }
        );
       //button5.setEnabled(false); 
        add(button5);
        button5.setBounds(260, 40, 80, 30);

        
        
        add(list1);
        list1.setBounds(20, 100, 150, 280);
        
        
        add(list2);
        list2.setBounds(220, 100, 160, 280);
        
        
        label3.setText("Array :");
        
        add(label3);
        label3.setBounds(20, 80, 100, 20);
        
        
        label4.setText("Array Ordenado :");
        
        add(label4);
        label4.setBounds(220, 80, 130, 20);

	label5.setText("TIempo : mmmmmmmmmm ms");
	add(label5);
	label5.setBounds(20,380,300,20);
        
    }


  private void InsertionSort(int Array[]) {

        int Key,i;

        for (int j=2;j< Array.length; j++){ 
                Key=Array[j];
                i = (j-1);
                while((i>0) && (Array[i]>=Key)){
                        Array[i+1]=Array[i];
                        i--;

                }
                Array[i+1]=Key;

        }

        
  }

  private void Generate(java.awt.event.ActionEvent evt) {

        list1.clear();
        for (int i =1;i <array.length;i++){
                array[i]=(int)(Math.random()*(new Integer(textField2.getText())).intValue()); // Cambiar 1000 por argumento en textfield2
                list1.add(""+array[i]);
        }
  }

  private int isNuma(java.awt.event.KeyEvent evt) {

      // Checar que sea un numero !
      if ((evt.getKeyChar()<48)||(evt.getKeyChar()>57)){
        return -1;

      }
      return 0;
      
  }

private void MergeSort(int Array[], int start, int len){
      int uno,dos;


      if (len > 1){
	uno = len / 2;dos = len - uno;
	MergeSort(Array, start, uno); // Rama Izquierda
	MergeSort(Array, start + uno, dos); // Rama derecha
	Merge(Array, start, uno, dos);
   }
} 

  

private void Merge(int Array[], int first, int uno, int dos){
	int t[] = new int[uno+dos]; // Tamano del array mezclado
	int dup ,dup1,dup2; // Muithox Contadoigres !
	dup = dup1 = dup2 = 0;

	
	while ((dup1 < uno) && (dup2 < dos)){ // Todavia no amanece y ya tengo sueno....
		if (Array[first + dup1] < Array[first + uno + dup2]){
			t[dup++] = Array[first + (dup1++)];
		}else{
			t[dup++] = Array[first + uno + (dup2++)];
		}
	}

	while (dup1 < uno) // Una mita
		t[dup++] = Array[first + (dup1++)];

	while (dup2 < dos) // Ota mitasd
		t[dup++] = Array[first + uno + (dup2++)];


	for (int g = 0; g < t.length; g++){ // Arriba la Luna oea !
		Array[first + g] = t[g];
	}
	}

   

}

/*
When the night... has come... and land is cold,

and the moon is the only, light we see

no I won't ! be afrid ! no I won't won't be afraid

just as long as Mathilda has a screen,

and My MP3 player keeps on going till I get sleep

hey darling darling stay on all night

just as looooong, as I get rid, of this homework...

hey keyboard keyboard stay, making love,

making love to mathilda just to help

this bored guy to finish his womework....

se he can... keep on going with ICQ (not in portuguese hehehe....)

-- FrESh

http://www.udlap.mx/~is111936/


*/

