Compare commits
	
		
			2 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 47473b618c | ||
|  | 5e4278e219 | 
							
								
								
									
										
											BIN
										
									
								
								Ex2/Exercice2.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Ex2/Exercice2.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								Ex3/Exercice3.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Ex3/Exercice3.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								Ex4/EX4.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Ex4/EX4.pdf
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										89
									
								
								Ex4/Exercice4.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								Ex4/Exercice4.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,89 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
|  | ||||
| // Fonction pour trier un tableau 1D | ||||
| void sort_1d(int* array, int length) { | ||||
|     for (int i = 0; i < length - 1; i++) { | ||||
|         for (int j = 0; j < length - i - 1; j++) { | ||||
|             if (array[j] > array[j + 1]) { | ||||
|                 int temp = array[j]; | ||||
|                 array[j] = array[j + 1]; | ||||
|                 array[j + 1] = temp; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| // Fonction pour calculer la somme d'un tableau 1D | ||||
| int sum_1d(int* array, int length) { | ||||
|     int sum = 0; | ||||
|     for (int i = 0; i < length; i++) { | ||||
|         sum += array[i]; | ||||
|     } | ||||
|     return sum; | ||||
| } | ||||
|  | ||||
| // Fonction pour trier un tableau 2D basé sur la somme des lignes | ||||
| void sort_2d(int** array, int rows, int cols) { | ||||
|     // Trier chaque ligne individuellement | ||||
|     for (int i = 0; i < rows; i++) { | ||||
|         sort_1d(array[i], cols); | ||||
|     } | ||||
|  | ||||
|     // Trier les lignes selon leur somme | ||||
|     for (int i = 0; i < rows - 1; i++) { | ||||
|         for (int j = 0; j < rows - i - 1; j++) { | ||||
|             if (sum_1d(array[j], cols) > sum_1d(array[j + 1], cols)) { | ||||
|                 int* temp = array[j]; | ||||
|                 array[j] = array[j + 1]; | ||||
|                 array[j + 1] = temp; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| // Fonction pour afficher un tableau 2D | ||||
| void print_2d(int** array, int rows, int cols) { | ||||
|     for (int i = 0; i < rows; i++) { | ||||
|         for (int j = 0; j < cols; j++) { | ||||
|             printf("%d ", array[i][j]); | ||||
|         } | ||||
|         printf("\n"); | ||||
|     } | ||||
| } | ||||
|  | ||||
| int main() { | ||||
|     // Exemple : Tableau 2D | ||||
|     int rows = 3, cols = 3; | ||||
|     int** array = (int**)malloc(rows * sizeof(int*)); | ||||
|     for (int i = 0; i < rows; i++) { | ||||
|         array[i] = (int*)malloc(cols * sizeof(int)); | ||||
|     } | ||||
|  | ||||
|     // Initialiser le tableau (exemple donné) | ||||
|     int data[3][3] = {{0, 3, 2}, {9, 4, 5}, {4, 1, 3}}; | ||||
|     for (int i = 0; i < rows; i++) { | ||||
|         for (int j = 0; j < cols; j++) { | ||||
|             array[i][j] = data[i][j]; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     // Affichage du tableau original | ||||
|     printf("Tableau original :\n"); | ||||
|     print_2d(array, rows, cols); | ||||
|  | ||||
|     // Trier le tableau | ||||
|     sort_2d(array, rows, cols); | ||||
|  | ||||
|     // Affichage du tableau trié | ||||
|     printf("\nTableau trié :\n"); | ||||
|     print_2d(array, rows, cols); | ||||
|  | ||||
|     // Libérer la mémoire | ||||
|     for (int i = 0; i < rows; i++) { | ||||
|         free(array[i]); | ||||
|     } | ||||
|     free(array); | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
							
								
								
									
										47
									
								
								Ex4/Exercice4.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								Ex4/Exercice4.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| def bubble_sort(array): | ||||
|     """ | ||||
|     Implémentation du tri à bulles pour un tableau 1D. | ||||
|     """ | ||||
|     n = len(array) | ||||
|     for i in range(n): | ||||
|         for j in range(0, n - i - 1): | ||||
|             if array[j] > array[j + 1]: | ||||
|                 array[j], array[j + 1] = array[j + 1], array[j] | ||||
|  | ||||
|  | ||||
| def bubble_sort_by_key(array, key_func): | ||||
|     """ | ||||
|     Implémentation du tri à bulles pour un tableau 2D ou supérieur | ||||
|     basé sur une fonction clé. | ||||
|     """ | ||||
|     n = len(array) | ||||
|     for i in range(n): | ||||
|         for j in range(0, n - i - 1): | ||||
|             if key_func(array[j]) > key_func(array[j + 1]): | ||||
|                 array[j], array[j + 1] = array[j + 1], array[j] | ||||
|  | ||||
|  | ||||
| def sort_nd_array(array): | ||||
|     """ | ||||
|     Trie récursivement un tableau multidimensionnel. | ||||
|     """ | ||||
|     if isinstance(array[0], list): | ||||
|         # Trier récursivement chaque sous-dimension | ||||
|         for sub_array in array: | ||||
|             sort_nd_array(sub_array) | ||||
|         # Trier cette dimension en fonction de la somme des sous-tableaux | ||||
|         bubble_sort_by_key(array, key_func=lambda x: sum(sum(y) if isinstance(y, list) else y for y in x)) | ||||
|     else: | ||||
|         # Si c'est un tableau 1D, appliquer le tri à bulles | ||||
|         bubble_sort(array) | ||||
|  | ||||
|  | ||||
| # Exemple pour un tableau 2D | ||||
| array_2d = [[0, 3, 2], [9, 4, 5], [4, 1, 3]] | ||||
| sort_nd_array(array_2d) | ||||
| print("Tableau trié 2D :", array_2d)  # Résultat attendu : [[0, 2, 3], [1, 3, 4], [4, 5, 9]] | ||||
|  | ||||
| # Exemple pour un tableau 3D | ||||
| array_3d = [[[3, 2], [1, 4]], [[9, 8], [2, 1]], [[5, 5], [3, 3]]] | ||||
| sort_nd_array(array_3d) | ||||
| print("Tableau trié 3D :", array_3d) | ||||
		Reference in New Issue
	
	Block a user