//Algoritmo que toma un arreglo y lo ordena usando el metodo de Minsort.
// y muestra el tiempo promedio que gasta en ordenar
//Guillermo Escobar P cod  256243
   import java.awt.Graphics;
   import java.applet.Applet;
   import java.applet.*;
   import java.awt.*;
   import java.util.Random;
  

   public class MiMinSort4 extends Applet 
   {
      TextField  num,result,salida2,salida1;
	 private Color c;
     int N;
	 private  int Arreglo[] = new int[N]; 
     private  int Array[]=new int [N];

   public  int H;
    
   public  void init()
      { num = new TextField(5);
		result = new TextField(5);
		salida1=new TextField(20);

		salida2=new TextField(20);
	     add(num);
		 add(result);
		 add(salida1);
		 add(salida2);
	      setLayout(null);
	     num.reshape(260,50,80,25);
		 result.reshape(260,270,80,25);
		 salida1.reshape(10,130,350,25); 
		 salida2.reshape(10,200,350,25);
	
	    }  
       
		 
		int[] minsort(int Array[], int N) 
         {
         this.Array = Array;
   
             for( int i = 0; i <N ; i++ )
               {
                swap(i, findMin(i, N));
               }
			return  Array;		
	      }
           
		 /**
    *  findMin(M, N) encuentra el indice del minimo entre 
    *  array[M], array[M+1], ...., array[N-1].
   **/
	  
		  int findMin(int minimo, int N)
              {//por defecto el elemento en minimo es el mas pequeno
          
               for( int j = minimo+1; j < N; j++ )
               {
                 if( Array[j] < Array[minimo] )
                {
                    minimo = j;	// el valor mas pequeno encontrado
                 }
                }  //fin for
              return minimo;
           }
   
	/**
    *  swap(i, j) intercambia los valores en  array[i]  y array[j]
   **/

	
    void swap(int i, int j)
    {
    int temp = Array[i];
    Array[i] = Array[j];
    Array[j] = temp;
    }
    
				
       
  public void paint( Graphics g )
    { 
	  g.setColor( c );
      setBackground(Color.orange);
      g.drawString ("Programa que halla el tiempo promedio del minsort",15,20);  
	 g.drawString ("Escriba el numero de Valores a ordenar", 5,65);  
     g. drawString("Valores iniciales a ordenar",5,120 );
      g.drawString("Valores ordenados usando Minsort",5,180); 
	  g.drawString("Este ordenamiento con minsort gasto un tiempo promedio de ",5,250);
	 	       	    
	   }
   
   
    public boolean action(Event e,Object o)
      	{   Graphics g;
			g=getGraphics();
			int N;
			int x=25;
			double D; 
	    
			if(e.target==num)
			{
		     N=Integer.parseInt(num.getText());
			 Random r = new Random();
            int Arreglo[] = new int[N]; 
             int Array[]=new int [N];
		     				
              for(int k =0;k<=N-1; k++ )
              {
                Arreglo[k] = r.nextInt(100);
                Array[k]=Arreglo[k];
		        print1(g," ",Array,15,100);	
           
				x=x+25;
			  			              						
			  }//fin for
	   	print2(g, " ",minsort(Array,N),15,180 );
			D=calculaAVG(N);
			result.setText(Double.toString(D)) ;
		    clear();	
			            
			  return true;
			  
           }//fin if
    
			  
	
  	return false;	  
	   }		 
				 
		    
      public void print1( Graphics g, String head, int b[],int x, int y )
      { 				 
		String texto=new String();
           g.drawString( head, x, y ); 
           x += 15;
           y += 20; 
           
           for ( int i = 0; i < b.length; i++ ) 
      	  {
           	texto+=(String.valueOf(b[i])+"  ");
            x =x+25;
				
            } 
	  		salida1.setText(texto);	
		 				//fin for
        }//fin print1   
		
		 public void print2( Graphics g, String head, int b[],int x, int y )
      { 				 
		String texto=new String();
           g.drawString( head, x, y ); 
           x += 15;
           y += 20; 
           
           for ( int i = 0; i < b.length; i++ ) 
      	  {
         	texto+=(String.valueOf(b[i])+"  ");
            x =x+25;
		   } //fin for
	  			
			salida2.setText(texto);	
	    }//fin print2   
		

	
	//
	double calculaAVG(int N)
	  {  double AVG;
	  if(N==0 ||N==1)
	    AVG=0;
		 else
       {  AVG=0.7434*N*N-0.68509*N-0.1278;}
	    return AVG;
      }
	 
	 void clear()
	 	{num.setText("");
       
		}  
  	
	
   }



 





































