1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| public class Tri extends JFrame { // frame= crée des fenêtres
protected static JMenuBar mb = new JMenuBar(); // barre de menu fourni par awt
protected static JMenu menuTris=new JMenu("Tris");
protected static JMenuItem miTriIns =new JMenuItem("Tri par insert.");
protected static JMenuItem miTriRapide =new JMenuItem("Quicksort");
int[] atrier= new int[10];
Object[] atrier2= new Object[10];
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
public Tri(){
super("Tri");
setJMenuBar(mb);
mb.add(menuTris);
menuTris.add(miTriIns);
menuTris.add(miTriRapide);
miTriIns.addActionListener(new ActionTriInsertion());
pack();
setVisible(true);
}
class ActionTriInsertion implements ActionListener {
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < 10; ++i){
atrier[i] = (int)(Math.random() * 128);
}
for(int y = 0; y < 10; ++y){
atrier2[y] = new Integer(atrier[y]);
}
model.setColumnCount(10);
model.addRow(atrier2);
add(table);
int j;
int v;
for (int i = 1; i < atrier.length; ++i) {
v = atrier[i];
j = i;
while (j > 0 && atrier[j-1] > v) {
atrier[j] = atrier[j-1];
--j;
}
atrier[j] = v;
for(int y = 0; y < 10; ++y){
atrier2[y] = new Integer(atrier[y]);
}
model.addRow(atrier2);
pack();
/* try {
Thread.sleep(2000);
pack();
}
catch (InterruptedException x) {} */
}
}
}
public static void main(String [] args){
new Tri();
}
} |
Partager