diff --git a/bubblesort.c b/bubblesort.c index a848827..1e88188 100644 --- a/bubblesort.c +++ b/bubblesort.c @@ -17,5 +17,5 @@ void bubblesort(int* array, int length) swapped++; } } - } while(swapped==1); + } while(swapped>0); } diff --git a/gmon.out b/gmon.out new file mode 100644 index 0000000..f119928 Binary files /dev/null and b/gmon.out differ diff --git a/quicksort.c b/quicksort.c new file mode 100644 index 0000000..b7365f3 --- /dev/null +++ b/quicksort.c @@ -0,0 +1,38 @@ +#include "quicksort.h" + +// Fonction utilitaire : échanger deux entiers +static void swap(int* a, int* b) +{ + int tmp = *a; + *a = *b; + *b = tmp; +} + +// Partition : divise le tableau autour du pivot +static int partition(int* A, int lo, int hi) +{ + int pivot = A[hi]; // pivot = dernier élément + int i = lo; + + for(int j = lo; j < hi; j++) + { + if(A[j] <= pivot) + { + swap(&A[i], &A[j]); + i++; + } + } + swap(&A[i], &A[hi]); // placer le pivot à la bonne position + return i; // index final du pivot +} + +// Quicksort récursif +void quicksort(int* A, int lo, int hi) +{ + if(lo >= hi || lo < 0) // vérification des bornes + return; + + int p = partition(A, lo, hi); + quicksort(A, lo, p - 1); // tri gauche + quicksort(A, p + 1, hi); // tri droite +} \ No newline at end of file diff --git a/quicksort.h b/quicksort.h new file mode 100644 index 0000000..a905b8e --- /dev/null +++ b/quicksort.h @@ -0,0 +1,6 @@ +#ifndef QUICKSORT_H +#define QUICKSORT_H + +void quicksort(int* array, int left, int right); + +#endif diff --git a/reponses.txt b/reponses.txt new file mode 100644 index 0000000..703ea62 --- /dev/null +++ b/reponses.txt @@ -0,0 +1,23 @@ +Commande utilisé : time ./student_rank 1000 1000 0 +~= 2.8s + +gprof ./student_rank + +Flat profile: temps et nombre d'appel par fonction + +Call graph : graphe d'appel, chaque fonction appel quel autre fonction et qui la appelé. Avec chaque métriques, nombre d'appel, temps. + +Grâce aux metriques, on peut voir que la fonction bubblesort est la plus lente visible dans le flat profile. C'est à cause d'un appel important dans find_rank_student visible dans le call graph. + +dans bubblesort mettre : +while(swapped>0); +Pour savoir si le tableau a bulle n'est pas subit de modification +(correction) + +suppression du 2eme bubblesort(grades,students_number); +bubblesort est beaucoup moins appelé + +remplacement de bubblesort avec heapsort +-> 0.107 (temps beaucoup plus court) + +Pour quicksort, pour qu'il soit meilleur, il faudrait faire un multi thread, pour qu'il puisse trier plusieur parti du tableau en même temps. \ No newline at end of file diff --git a/student_rank b/student_rank new file mode 100755 index 0000000..a25da98 Binary files /dev/null and b/student_rank differ diff --git a/student_rank.c b/student_rank.c index af84003..5f798cd 100644 --- a/student_rank.c +++ b/student_rank.c @@ -5,6 +5,7 @@ #include #include "heapsort.h" #include "bubblesort.h" +#include "quicksort.h" void generate_grades(int** students_array, int students_number, int grades_number) { @@ -55,14 +56,12 @@ int find_rank_student(int student_grade, int* grades_array, int students_number) { int position = -1; int i = 0; - bubblesort(grades_array,students_number); for(i = students_number-1; i >= 0; i--) { - if(grades_array[i] == student_grade) - { + if(grades_array[i] == student_grade){ position = students_number-i; - break; - } + break; + } } return position; } @@ -77,7 +76,8 @@ void sort_students(int** students_rank, int** students_array, int students_numbe { grades[j] = students_array[j][i]; } - bubblesort(grades,students_number); + quicksort(grades, 0, students_number - 1); + for(j = 0; j < students_number; j++) { students_rank[j][i] = find_rank_student(students_array[j][i],grades,students_number); @@ -86,6 +86,8 @@ void sort_students(int** students_rank, int** students_array, int students_numbe } } + + int main(int argc, char** argv) { int** student_grades = NULL;