Compare commits

1 Commits
main ... main

Author SHA1 Message Date
5f5041663e tp1 2025-09-15 14:34:48 +02:00
7 changed files with 76 additions and 7 deletions

View File

@@ -17,5 +17,5 @@ void bubblesort(int* array, int length)
swapped++;
}
}
} while(swapped==1);
} while(swapped>0);
}

BIN
gmon.out Normal file

Binary file not shown.

38
quicksort.c Normal file
View File

@@ -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
}

6
quicksort.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef QUICKSORT_H
#define QUICKSORT_H
void quicksort(int* array, int left, int right);
#endif

23
reponses.txt Normal file
View File

@@ -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.

BIN
student_rank Executable file

Binary file not shown.

View File

@@ -5,6 +5,7 @@
#include <stdlib.h>
#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;