ORDENAR UN ARRAY DE NÚMEROS (MÉTODO DE BURBUJA)

 

 

Se crea una aplicación tipo Applet en la que el nombre de la pagina puede ser:

Ordenacionapplet.html y el código contendrá una llamada al applet ordenar.java

 

<HTML>

<HEAD>

<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">

</HEAD>

<BODY>

 

<P>&nbsp;</P>

 

 

<!-- Insert HTML here -->

                <applet

                               code=ordenar.class

                               name=ordenar1

                               width=320

                               height=200  VIEWASTEXT>

                               <param name=label value="This string was passed from the HTML host.">

                               <param name=background value="008080">

                               <param name=foreground value="FFFFFF">

                </applet>

 

</BODY>

</HTML>

 

 

 

 

 

 

El código de la clase será el siguiente:

 

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import java.util.*;

 

public class ordenar extends Applet implements ActionListener, Runnable

{

                private static int numelems =100;

                private static int rango =160;

                private int[] arrOrd;

                private Thread t = null;

                private final int ancho =640;

                private final int alto =400;

                private boolean ejecutable = true;

               

                private static Color colordeBarra= Color.blue;

                private static int anchuradeBarra= 3;

                private static int yIni = 100 + rango;

                private static int xIni = 20;

                private static int xSep = anchuradeBarra*2;

                private Image imagen;

                private Graphics contextoGraf;

               

                Panel p1;

                Button nuevoArr;

                Button nuevaOrd;

               

                public ordenar()

                {

                               setLayout(new BorderLayout());

                               p1 = new Panel();

                               p1.setLayout(new FlowLayout());

                               nuevoArr = new Button("Nuevo Array");

                               nuevaOrd = new Button("Ordenar");

                               nuevoArr.addActionListener(this);

                               nuevaOrd.addActionListener(this);

                               p1.add(nuevoArr);

                               p1.add(nuevaOrd);

                               add("South",p1);

                               arrOrd = randomEntArray(numelems);

                }

               

                public int[] randomEntArray(int N)

                {

                               Random gen = new Random();

                               int[] a = new int[N];

                               for(int i= 0 ; i<a.length;i++)

                                               a[i] = Math.abs(gen.nextInt()%rango);

                               return(a);

                }

                              

                public void init()

                {

                               resize(ancho, alto);

                               try

                               {

                                               imagen = createImage(ancho,alto);

                                               contextoGraf = imagen.getGraphics();

                               }

                               catch(Exception e)

                               {

                                               contextoGraf = null;

                               }

                }

               

                public void paint(Graphics g)

                {

                               if(contextoGraf!= null)

                               {

                                               dibuja(contextoGraf);

                                               g.drawImage(imagen,0,0,this);

                               }

                               else

                                               dibuja(g);

                }

                public void dibuja(Graphics g)

                {

                               g.setColor(Color.gray);

                               g.fillRect(0,0,ancho,alto);

                               for(int i=0;i<arrOrd.length;i++)

                                               dibujaEnt(g,arrOrd[i],i);

                }

                public void dibujaEnt(Graphics g, int h, int i)

                {

                               int x = xIni + i*xSep;

                               int y = yIni - h;

                               int w = anchuradeBarra;

                               g.setColor(colordeBarra);

                               g.fillRect(x,y,w,h);

                }

                              

                public void update(Graphics g)

                {             

                               paint(g);

                }

                public void actionPerformed(ActionEvent e)

                {

                               if(e.getSource() == nuevoArr)

                               {

                                               if(t!=null)

                                                               hilostop();

                                               arrOrd=randomEntArray(numelems);

                                               repaint();

                               }

                               if(e.getSource()==nuevaOrd)

                               {

                                               if(t== null)

                                                               t= new Thread(this);

                                               ejecutable = true;

                                               t.start();

                               }

                }

                public void hilostop()

                {

                               ejecutable= false;

                               t= null;

                }

                public void stop()

                {

                               hilostop();

                }

                public void run()

                {

                               while(t!=null)

                               {

                                               for(int i=1; i<arrOrd.length && ejecutable;i++)

                                                               for(int j=0; j < (arrOrd.length-1) && ejecutable; j++)

                                                                              if(arrOrd[j+1] < arrOrd[j] && ejecutable)

                                                                              {

                                                                                              int temp = arrOrd[j];

                                                                                              arrOrd[j] = arrOrd[j+1];

                                                                                              arrOrd[j+1]= temp;

                                                                                              repaint();

                                                                                              try

                                                                                              {

                                                                                                              t.sleep(1);

                                                                                              }

                                                                                              catch(Exception e)

                                                                                              {

                                                                                              }

                                                                              }

                                               t= null;

                               }

                }

}