/* * Fichier: TriParSelection.java * Crée le: 04 janvier 2007. * Modifié: 7 juillet 2007. * Auteurs: Sébastien ESTIENNE. * SiteWeb: http://www.prog-info.org/ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ package chapitre6; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; /** *
Effectue un tri par sélection.
* @author Sébastien ESTIENNE. */ public class TriParSelection extends JFrame { /** *Serial version UID.
*/ private static final long serialVersionUID = 1L; /** *Construction de l'application.
*/ public TriParSelection() { // Appel du constructeur de la classe JFrame. super("Tri Par Sélection"); // Zone de texte de sortie. JTextArea zoneSortie = new JTextArea(); zoneSortie.setEditable(false); // Conteneur. Container conteneur = getContentPane(); // Ajoute la zone de sortie au conteneur. conteneur.add(new JScrollPane(zoneSortie)); // Tableau des valeurs à trier. int[] tableau = {5, 56, 34, 43, 17, 25, 8, 65, 31, 52}; // Chaine de caractère de sortie. StringBuilder sortie = new StringBuilder(); // Ajoute à la chaine de sortie les valeurs de départ du tableau. sortie.append("Valeurs de départ du tableau:\n"); for(int i = 0; i < tableau.length; i++) { sortie.append(" ").append(tableau[i]); } // Trie le tableau. triParSelection(tableau); // Ajoute à la chaine de sortie les valeurs triées du tableau. sortie.append("\n\n"); sortie.append("Valeurs triées du tableau:\n"); for(int i = 0; i < tableau.length; i++) { sortie.append(" ").append(tableau[i]); } // Met à jour la zone de sortie. zoneSortie.setText(sortie.toString()); // Modifie les propriétés de la fenêtre. setSize(300, 150); setLocation(100, 100); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** *Effectue un tri par sélection.
* @param tableau Les valeurs à trier. */ public void triParSelection(int[] tableau) { // Parcours du tableau. for(int i = 0; i < tableau.length - 1; i++) { int min = i; // Recherche de l'indice minimum. for(int j = i + 1; j < tableau.length; j++) { if(tableau[j] < tableau[min]) { min = j; } } // Permutation. if(i != min) { permuter(tableau, i, min); } } } /** *Permutte deux éléments d'un tableau.
* @param tableau Le tableau concerné. * @param i L'indice du premier élément. * @param j L'indice du second élément. */ public void permuter(int[] tableau, int i, int j) { // Variable temporaire pour la permutation. int temp; temp = tableau[i]; tableau[i] = tableau[j]; tableau[j] = temp; } /** *Lance l'application.
* @param args Les paramètres de la ligne de commande. */ public static void main(String[] args) { new TriParSelection(); } }