IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

avec Java Discussion :

Tri d'une matrice par colonne


Sujet :

avec Java

  1. #1
    Membre à l'essai
    Inscrit en
    Janvier 2009
    Messages
    24
    Détails du profil
    Informations forums :
    Inscription : Janvier 2009
    Messages : 24
    Points : 20
    Points
    20
    Par défaut Tri d'une matrice par colonne
    Bonjour,

    Y a-t-il un moyen de trier la colonne d'une matrice et la réécrire avec les éléments de sa ligne:

    Je prends cet exemple:
    w =

    1 2 5
    8 7 9
    8 9 7
    En triant la 3ème colonne j'aimerais bien obtenir ceci:
    w =

    8 9 9
    8 7 7
    1 2 5
    J'ai fait ceci :
    nbreL: nombre de liste par exemple : 9 est une liste
    8
    1
    nbreO: nombre d’éléments par exemple :9
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    for (int k=0;k<nbreL;i++) {
     
       for (int i=0;i<nbreO;i++) {
     
             int elem = matrice[i][k];
     
             for ( j = i; j > 0 && matrice[j-1][k] < elem ; j--) {
     
                  matrice[j][k] = matrice[j-1];
             }
     
             matrice[j][k] = elem;
        }
    }
    Quelqu'un saurait-il m'indiquer comment faire ?

    Merci d'avance pour votre aide.

  2. #2
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Bonsoir,

    Utilise Arrays.sort()
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    int[][] matrice = {{1 ,2, 5},{8, 7, 9},{8, 9, 7}};
    int[][] matriceTrier = new int[3][3];
    for(int j = 0; j < matrice[0].length; j++){
    	for (int i = 0; i< matrice.length; i++){
    		matriceTrier[j][i] = matrice[i][j]; 
    	}
    	Arrays.sort(matriceTrier[j]);
    }
    for(int j = matriceTrier[0].length-1; j >=0 ; j--){
    	for (int i = 0; i< matriceTrier.length; i++){
    		matrice[j][i] = matriceTrier[i][j]; 
    	}
    }
    A+.

  3. #3
    Membre à l'essai
    Inscrit en
    Janvier 2009
    Messages
    24
    Détails du profil
    Informations forums :
    Inscription : Janvier 2009
    Messages : 24
    Points : 20
    Points
    20
    Par défaut
    salut
    merci pour avoir prie le temps de me répondre, je voudrais ajuster votre code pour une matrice n*n et trie les colonne

  4. #4
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    C'est le même.

  5. #5
    Membre à l'essai
    Inscrit en
    Janvier 2009
    Messages
    24
    Détails du profil
    Informations forums :
    Inscription : Janvier 2009
    Messages : 24
    Points : 20
    Points
    20
    Par défaut
    salut

    j'ai prie ton code comme il est il marche pas il trie n'import comment
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    import java.util.Arrays;
     
     
    public class TPe {
     
    	/**
             * @param args
             */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
     
     
    int[][] matrice = {{1 ,2, 5},{8, 7, 9},{8, 9, 7}};
    int[][] matriceTrier = new int[3][3];
    for(int j = 0; j < matrice[0].length; j++){
    	for (int i = 0; i< matrice.length; i++){
    		matriceTrier[j][i] = matrice[i][j]; 
    	}
    	Arrays.sort(matriceTrier[j]);
    }
    for(int j = matriceTrier[0].length-1; j >=0 ; j--){
    	for (int i = 0; i< matriceTrier.length; i++){
    		matrice[j][i] = matriceTrier[i][j]; 
    	}
    }
     
    		for(int j = 0; j < matrice[0].length; j++){
    			for (int i = 0; i< matrice.length; i++){
    		System.out.print(matrice[j][i]);
    		}
    			}
     
    	}
     
     
     
     
     
    }

  6. #6
    Modérateur
    Avatar de kolodz
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2008
    Messages
    2 211
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 211
    Points : 8 316
    Points
    8 316
    Billets dans le blog
    52
    Par défaut
    Je crois que si en fait !
    J'obtiens le résultat suivant :
    125
    877
    899
    Le tableau est donc trié, mais de manière croissante. Il suffit d'inverser les l'ordre au bon moment.
    Voici le code avec l'affichage de la matrice à chaque étape :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    import java.util.Arrays;
     
    public class TPe {
    	public static void reverse(int[] data) {
    		for(int i = 0; i < data.length; i++)  
            {  
                int temp = data[i];  
                data[i] = data[data.length - i - 1];  
                data[data.length - 1] = temp;
            } 
    	}
     
    	/**
             * @param args
             */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		int[][] matrice = { { 1, 2, 5 }, { 8, 7, 9 }, { 8, 9, 7 } };
    		displayMatrice(matrice);
    		matrice = inverserColonneLigne(matrice);
    		displayMatrice(matrice);
     
    		for (int j = 0; j < matrice[0].length; j++) {
    			Arrays.sort(matrice[j]);
    		}
    		displayMatrice(matrice);
    		for (int j = 0; j < matrice[0].length; j++) {
    			reverse(matrice[j]);
    		}
    		displayMatrice(matrice);
    		matrice = inverserColonneLigne(matrice);
    		displayMatrice(matrice);
    	}
     
    	private static void displayMatrice(int[][] matrice) {
    		for (int j = 0; j < matrice[0].length; j++) {
    			for (int i = 0; i < matrice.length; i++) {
    				System.out.print(matrice[j][i]);
    			}
    			System.out.println();
    		}
    		System.out.println();
    	}
     
    	private static int[][] inverserColonneLigne(int[][] matrice) {
    		int[][] matriceTrier = new int[3][3];
    		for (int j = 0; j < matrice[0].length; j++) {
    			for (int i = 0; i < matrice.length; i++) {
    				matriceTrier[j][i] = matrice[i][j];
    			}
    		}
    		return matriceTrier;
    	}
     
    }
    Si une réponse vous a été utile pensez à
    Si vous avez eu la réponse à votre question, marquez votre discussion
    Pensez aux FAQs et aux tutoriels et cours.

  7. #7
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Bonjour,

    J'avais désolé mais j'avais pas remarqué le denier post de begamine5.
    J'ai testé et ça marche, il suffit seulement d'inverser la matrice obtenue.
    Citation Envoyé par kolodz
    Le tableau est donc trié, mais de manière croissante. Il suffit d'inverser les l'ordre au bon moment.
    Il suffit seulement d'ajouter une boucle pour inverser la matrice:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    public static void main(String[] args) {
    	int[][] matrice = {{1 ,2, 5},{8, 7, 9},{8, 9, 7}};
    	matrice = inverseMatrice(matrice);
    	for (int i=0; i< matrice.length; i++){
    		for (int j=0; j<matrice[i].length; j++){
    			System.out.print(matrice[i][j]);
    		}
    		System.out.println();
    	}
    }
     
    public static int[][] inverseMatrice(int[][] matrice){
    	int[][] matriceTrier = new int[matrice.length][matrice[0].length];
    	for(int j = 0; j < matrice[0].length; j++){
    		for (int i = 0; i< matrice.length; i++){
    			matriceTrier[j][i] = matrice[i][j]; 
    		}
    		Arrays.sort(matriceTrier[j]);
    	}
    	for(int j = matriceTrier[0].length-1; j >=0 ; j--){
    		for (int i = 0; i< matriceTrier.length; i++){
    			matrice[j][i] = matriceTrier[i][j]; 
    		}
    	}
     
    	for(int j = matrice.length-1, i= 0; j >=0 ; j--, i++){
    			matriceTrier[i] = matrice[j]; 
    	}
    	return matriceTrier;
    }
    A+.

  8. #8
    Membre à l'essai
    Inscrit en
    Janvier 2009
    Messages
    24
    Détails du profil
    Informations forums :
    Inscription : Janvier 2009
    Messages : 24
    Points : 20
    Points
    20
    Par défaut
    salut

    merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant] Tri une matrice par rapport à un vecteur
    Par linda05 dans le forum MATLAB
    Réponses: 4
    Dernier message: 29/03/2014, 15h48
  2. trie d'une matrice par colonne
    Par bigamine5 dans le forum Débuter
    Réponses: 0
    Dernier message: 15/10/2012, 15h39
  3. trier une matrice par ses colonnes
    Par Alex999 dans le forum C#
    Réponses: 2
    Dernier message: 12/12/2011, 12h43
  4. Réponses: 1
    Dernier message: 19/03/2009, 14h44
  5. [VBA-E]Tri d'une plage par une colonne
    Par illight dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 07/02/2006, 10h53

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo