QSort.java_



package hola;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Descripcion:
 * Copyright:    Copyright (c) 2001
 * Empresa:
 * @author
 * @version 1.0
 */

public class Marco1 extends JFrame implements Runnable {
  JPanel contentPane;
  int []a={2,4,1,3,6,7,5,8,9,12,23,29,56,76,52,31,45,32,65,32,14,24,98};
  int b=1,c=1;
  int e=0,f=0, h=0;
  Thread Correr;

  BorderLayout borderLayout1 = new BorderLayout();

  /**Construir el marco*/
  public Marco1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    start();
  }
  private void jbInit() throws Exception  {
    //setIconImage(Toolkit.getDefaultToolkit().createImage(Marco1.class.getResource("[Icono]")));
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(borderLayout1);
    this.setSize(new Dimension(600, 500));
  }
  /**Modificado para poder salir cuando se cierra la ventana*/
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

   public void paint (Graphics g)
  {
     g.clearRect(0,0,700,600);
     pintar(g);
  }

 public void pintar(Graphics g)
   {
      e = 0;
  for(int n=0; n<a.length; n++)
   {
   h=a[n]*5;
    g.drawLine(0,e,h,e);
       e+=10;
   }
 }
 public void start()
	{
		if(Correr == null)
		{
                        Correr=new Thread(this);
			Correr.start();
		}
	}

	public void run()
	{
          Hola(a,0,a.length-1);
          repaint();
          try { Correr.sleep(1000); }
          catch(InterruptedException e){}
        }

  void Hola(int []arr, int l, int h)
  {
   if(l>=h || l<0 || h<0)
   return;
   int k=partition(arr,l,h);
   Hola(arr,l,k-1);
   Hola(arr, k+1,h);
  }
  //otros miembros

 public int partition(int []arr, int l,int h)
 {
  int i=l, j=h;
  // se elige un elemento
  swap(arr,(i+j)/2, h);    // el pivote se mueve a h
  int pe=arr[h];
  while (i<j)
  {
   while(i<j && arr[i] <= pe)  //a partir del lado izquierdo
    i++;
   while(i<j && arr[j] >=pe)   //a partir del lado derecho
    j--;
   if(i<j) swap(arr,i++,j);    //se intercambian elementos
  }
  if (i!=h) swap(arr,i,h);     //elemento pivote en su lugar
  return (i);
 }

 public void swap(int []a, int i, int j)
 {
  int tmp =a[i];
  a[i]=a[j];
  a[j]=tmp;
  repaint();
  try { Correr.sleep(100); }
  catch(InterruptedException e){}
 }
}