Compare commits

2 Commits
main ... main

Author SHA1 Message Date
vaisse
d2f5c7106d quicksort 2025-09-10 17:15:58 +02:00
vaisse
533d7685ca taf fait 2025-09-10 16:37:41 +02:00
7 changed files with 106 additions and 12 deletions

18
VAISSE_compte_rendu Normal file
View File

@@ -0,0 +1,18 @@
GPROF : FONCTIONNEMENT
Deux métriques sont utilisés lors des mesures : le temps, et le nombre d'appels.
Dans un premier tableau, dit Flat Profile, il donne la consommation du temps en pourcentage par rapport au temps d'exécution total, ainsi que le nombre d'appels sur ce même temps. Il fait cela en mesurant tous les centièmes de secondes.
D'après ce premier tableau, on constate que le tri bulle consomme nettement plus
que n'importe quelle autre fonction du programme, avec une consommation d'à peu près 80% du temps total d'exécution. La deuxième fonction consommant le plus est find_rank_student, avec une consommation de 20%.
On a aussi le callgraph, qui permet de hiérarchiser les fonctions selon qui appelle qui, en indiquant le nombre d'appels et les temps d'exécution individuels de chaque sous-fonctions.
Ici, on voit que la fonction bubblesort est appelé 1 million de fois par find_rank_student; c'est sûrement ce qui pose un problème.
Cela nous permet de voir qu'il y a un appel en trop de bubblesort dans find_rank_student. Une fois retiré, et Gprof relancé, bubblesort n'est appelé plus que mille fois. Mais, l'algorithme reste toujours trop lourd, et continue de consomer près de 80% du temps d'exécution total. En replaçant l'algorithme par un plus optimisé, comme le heapsort, les performances sont largement meilleures. Il n'y a plus qu'une dizaines d'opérations effectuées, et le temps de consommation est quasiment nulle pour toutes les fonctions.
L'ensemble de ces résultats sont valables pour 1000 étudiants et 1000 notes chacuns. Pour 10000 et 10000, même avec un tri par tas, l'exécution consomme bien plus.
On va donc devoir changer l'algorithmie.

View File

@@ -14,7 +14,7 @@ void bubblesort(int* array, int length)
tmp = array[i-1];
array[i-1] = array[i];
array[i] = tmp;
swapped++;
swapped=1;
}
}
} while(swapped==1);

BIN
gmon.out Executable file

Binary file not shown.

67
quicksort.c Normal file
View File

@@ -0,0 +1,67 @@
/*algorithm quicksort(A, lo, hi) is
// Ensure indices are in correct order
if lo >= hi || lo < 0 then
return
// Partition array and get the pivot index
p := partition(A, lo, hi)
// Sort the two partitions
quicksort(A, lo, p - 1) // Left side of pivot
quicksort(A, p + 1, hi) // Right side of pivot
// Divides array into two partitions
algorithm partition(A, lo, hi) is
pivot := A[hi] // Choose the last element as the pivot
// Temporary pivot index
i := lo
for j := lo to hi - 1 do
// If the current element is less than or equal to the pivot
if A[j] <= pivot then
// Swap the current element with the element at the temporary pivot index
swap A[i] with A[j]
// Move the temporary pivot index forward
i := i + 1
// Swap the pivot with the last element
swap A[i] with A[hi]
return i // the pivot index*/
int Partition(int * array, int lo, int hi){
/*variables*/
int pivot = array[hi];
int tmp_ind = lo;
int tmp;
int i = 0;
/*pivots*/
for(i=lo; i<hi; i++){
if(array[i]<=pivot){
tmp = array[i];
array[i] = array[tmp_ind];
array[tmp_ind] = tmp;
tmp_ind++;
}
}
tmp = array[tmp_ind];
array[tmp_ind] = array[hi];
array[hi] = tmp;
return tmp_ind;
}
void quicksort(int * array, int lo, int hi){
/*variables*/
int p;
/*test - indices dans l'ordre*/
if((lo>=hi)||(lo<0)){
return;
}
p = Partition(array, lo, hi);
quicksort(array, lo, p-1);
quicksort(array, p+1, hi);
}

7
quicksort.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef __QUICKSORT__
#define __QUICKSORT__
int Partition(int * array, int lo, int hi);
void quicksort(int * array, int lo, int hi);
#endif

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,7 +56,7 @@ int find_rank_student(int student_grade, int* grades_array, int students_number)
{
int position = -1;
int i = 0;
bubblesort(grades_array,students_number);
(grades_array,students_number);
for(i = students_number-1; i >= 0; i--)
{
if(grades_array[i] == student_grade)
@@ -73,16 +74,16 @@ void sort_students(int** students_rank, int** students_array, int students_numbe
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);
for(j = 0; j < students_number; j++)
{
grades[j] = students_array[j][i];
}
quicksort(grades,0,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);
}
}
@@ -93,6 +94,7 @@ int main(int argc, char** argv)
int students_length = 0;
int grades_length = 0;
int debug_mode = 0;
int* ranker = NULL;
if(argc != 4)
{