forked from menault/TD4_DEV51_Qualite_Algo
ajout TD4 complet
This commit is contained in:
52
tri_tableau.c
Normal file
52
tri_tableau.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int comparer(const void *a, const void *b) {
|
||||
return (*(int*)a - *(int*)b);
|
||||
}
|
||||
|
||||
int somme(int *tab, int taille) {
|
||||
int s = 0;
|
||||
for (int i = 0; i < taille; i++) {
|
||||
s += tab[i];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
// Tri principal selon la somme des sous-tableaux
|
||||
void trierTableau(int **tab, int n, int m) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
qsort(tab[i], m, sizeof(int), comparer);
|
||||
}
|
||||
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (somme(tab[i], m) > somme(tab[j], m)) {
|
||||
int *temp = tab[i];
|
||||
tab[i] = tab[j];
|
||||
tab[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n = 3, m = 3;
|
||||
|
||||
int ligne1[] = {0, 3, 2};
|
||||
int ligne2[] = {9, 4, 5};
|
||||
int ligne3[] = {4, 1, 3};
|
||||
|
||||
int *tableau[] = {ligne1, ligne2, ligne3};
|
||||
|
||||
trierTableau(tableau, n, m);
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
printf("%d ", tableau[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user