From 6cdd2b4fd12f1cc2118bf9aa962f73cdbd35c9c7 Mon Sep 17 00:00:00 2001 From: thuret Date: Tue, 3 Sep 2024 11:33:17 +0200 Subject: [PATCH] ajout changement --- modif/bubblesort.c | 21 ++++++++ modif/heapsort.c | 80 +++++++++++++++++++++++++++ modif/notes.md | 7 ++- modif/student_rank.c | 125 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 231 insertions(+), 2 deletions(-) create mode 100644 modif/bubblesort.c create mode 100644 modif/heapsort.c create mode 100644 modif/student_rank.c diff --git a/modif/bubblesort.c b/modif/bubblesort.c new file mode 100644 index 0000000..a848827 --- /dev/null +++ b/modif/bubblesort.c @@ -0,0 +1,21 @@ +// Bubblesort Algorithm +// M.Menault 2024 + +void bubblesort(int* array, int length) +{ + int swapped, i, tmp; + do + { + swapped = 0; + for(i=1;i array[i]) + { + tmp = array[i-1]; + array[i-1] = array[i]; + array[i] = tmp; + swapped++; + } + } + } while(swapped==1); +} diff --git a/modif/heapsort.c b/modif/heapsort.c new file mode 100644 index 0000000..668f4b7 --- /dev/null +++ b/modif/heapsort.c @@ -0,0 +1,80 @@ +// Heapsort Algorithm +// M.Menault 2024 + +#include +#include +#include + +void print_array(int* array, int length) +{ + static int count = 0; + int i = 0; + printf("(%3.3d) Array : ",count++); + for(i=0; i < length; i++) + { + printf("%5d ",array[i]); + } + printf("\n"); +} + +void sift(int* array, int node, int length) +{ + int largest_node = node; + int left_node = 2*node+1; + int right_node = 2*node+2; + int temp_value = 0; + + if(left_node < length && array[left_node] > array[largest_node]) + { + largest_node = left_node; + } + + if(right_node < length && array[right_node] > array[largest_node]) + { + largest_node = right_node; + } + + if(largest_node != node) + { + temp_value = array[node]; + array[node] = array[largest_node]; + array[largest_node] = temp_value; + sift(array,length,largest_node); + } +} + +void heapsort(int* array, int length) +{ + int i = 0; + int temp_value = 0; + + // Sift the current array (binary tree) + for(i=(length/2); i >= 0; i--) + { + sift(array,i,length); + } + // Heapsort ! + for(i=length-1; i > 0; i--) + { + temp_value = array[i]; + array[i] = array[0]; + array[0] = temp_value; + sift(array,0,i); + } +} + +void generate_array(int* array, int length) +{ + int i = 0; + static int first_call = 1; + + if(first_call) + { + first_call = 0; + srand(time(NULL)); + } + for(i=0; i < length; i++) + { + array[i] = rand() % 20 + 1; + } +} diff --git a/modif/notes.md b/modif/notes.md index 217bfb2..737c970 100644 --- a/modif/notes.md +++ b/modif/notes.md @@ -161,6 +161,9 @@ main
## optimisation du programme : ## -- réduire les appel de bubblesort +- réduire les appel de bubblesort - optimiser bubbesort (plus complexe) -- réduire les appel de sort_student car elle appelle les autres fonctions \ No newline at end of file +- réduire les appel de sort_student car elle appelle les autres fonctions + +### changmenent effectué : ### +suppression de la ligne 58 de student_rank.c \ No newline at end of file diff --git a/modif/student_rank.c b/modif/student_rank.c new file mode 100644 index 0000000..5ab0716 --- /dev/null +++ b/modif/student_rank.c @@ -0,0 +1,125 @@ +// Student rank +// M.Menault 2024 + +#include +#include +#include "heapsort.h" +#include "bubblesort.h" + +void generate_grades(int** students_array, int students_number, int grades_number) +{ + int i = 0; + for(i=0;i= 0; i--) + { + if(grades_array[i] == student_grade) + { + position = students_number-i; + break; + } + } + return position; +} + +void sort_students(int** students_rank, int** students_array, int students_number, int grades_number) +{ + int i = 0, j = 0; + for(i = 0; i < grades_number; i++) + { + int * grades = (int*) malloc(students_number*sizeof(int)); + for(j = 0; j < students_number; j++) + { + grades[j] = students_array[j][i]; + } + bubblesort(grades,students_number); + for(j = 0; j < students_number; j++) + { + students_rank[j][i] = find_rank_student(students_array[j][i],grades,students_number); + } + free(grades); + } +} + +int main(int argc, char** argv) +{ + int** student_grades = NULL; + int** student_ranks = NULL; + int students_length = 0; + int grades_length = 0; + int debug_mode = 0; + + if(argc != 4) + { + printf("Usage : %s \n",argv[0]); + return -1; + } + students_length = atoi(argv[1]); + grades_length = atoi(argv[2]); + debug_mode = atoi(argv[3]); + + student_grades = (int**) malloc(students_length*sizeof(int*)); + student_ranks = (int**) malloc(students_length*sizeof(int*)); + generate_grades(student_grades,students_length,grades_length); + generate_ranks(student_ranks,students_length,grades_length); + + sort_students(student_ranks,student_grades,students_length,grades_length); + + if(debug_mode) + { + print_student_array(student_grades,students_length,grades_length); + print_student_array(student_ranks,students_length,grades_length); + } + + free_array(student_grades,students_length); + free_array(student_ranks,students_length); + free(student_grades); + free(student_ranks); + return 0; +} +