Commit 828c0583 authored by Antoine Sauray's avatar Antoine Sauray
Browse files

finished tp

parent ec065d16
......@@ -4,6 +4,11 @@
#include <stdbool.h>
#include <stdio.h>
#include "utils.h"
void quicksort(int *tab, int debut, int fin, bool (*f1Ptr)(int, int), bool (*f2Ptr)(int, int));
void testQuickSortAscendant(int* tab, int size);
void testQuickSortDescendent(int* tab, int size);
#endif
\ No newline at end of file
......@@ -5,17 +5,23 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
static const int ASCENDING=1, DESCENDING=-1;
typedef void (*algorithm)(int*, int);
void printArray(int* array, int size);
void init(int* array, int size);
bool isSorted(int* array, int size, int order);
// Après un speedtest faire un free
int* speedTest(int* array, int size, algorithm* sortingAlgos, int nbAlgorithms);
bool more(int a,int b);
bool less(int a,int b);
bool descending(int a, int b);
bool pair(int a, int b);
bool impair(int a, int b);
......
#include "../include/main.h"
#define SIZE 10
#define SIZE 10000
int main(){
srand(time(NULL));
......@@ -13,6 +13,10 @@ int main(){
printf("array is not sorted\n");
}
printArray(rands, SIZE);
/*
Fonctions de comparaison
*/
bool (*morePtr)(int,int);
morePtr = &more;
......@@ -20,7 +24,35 @@ int main(){
bool (*lessPtr)(int,int);
lessPtr = &less;
bool (*pairPtr)(int,int);
pairPtr = &pair;
bool (*impairPtr)(int,int);
impairPtr = &impair;
/*
Test d'un QuickSort ordre croissant
*/
quicksort(rands, 0, SIZE, lessPtr, morePtr);
printArray(rands, SIZE);
/*
Test de fonctions de tri
*/
const int nbAlgorithms=2;
algorithm tests[nbAlgorithms];
tests[0]=testQuickSortAscendant;
tests[1]=testQuickSortDescendent;
int* res = speedTest(rands, SIZE, tests, nbAlgorithms);
for(int i=0;i<nbAlgorithms;i++){
printf("Algorithm[%d] : completed in %d ms\n", i, res[i]);
}
free(res);
return 0;
}
......@@ -28,5 +28,24 @@ void quicksort(int *tab, int debut, int fin, bool (*f1Ptr)(int, int), bool (*f2P
//récursion
quicksort(tab, debut, droite, f1Ptr, f2Ptr);
quicksort(tab, droite+1, fin, f1Ptr, f2Ptr);
}
void testQuickSortAscendant(int* tab, int size){
bool (*morePtr)(int,int);
morePtr = &more;
bool (*lessPtr)(int,int);
lessPtr = &less;
quicksort(tab, 0, size, lessPtr, morePtr);
}
void testQuickSortDescendent(int* tab, int size){
bool (*morePtr)(int,int);
morePtr = &more;
bool (*lessPtr)(int,int);
lessPtr = &less;
quicksort(tab, 0, size, morePtr, lessPtr);
}
\ No newline at end of file
......@@ -21,6 +21,22 @@ bool isSorted(int* array, int size, int order){
}
return true;
}
int* speedTest(int* array, int size, algorithm* sortingAlgos, int nbAlgorithms){
int* msec = malloc(nbAlgorithms*sizeof(int));
clock_t before = 0;
clock_t difference=0;
for(int i=0;i<nbAlgorithms;i++){
before = clock();
(*sortingAlgos[i])(array, size);
difference = clock() - before;
msec[i] = difference * 1000 / CLOCKS_PER_SEC;
}
return msec;
}
bool more(int a, int b){
//printf("%d < %d \n", a, b);
......@@ -30,8 +46,8 @@ bool less(int a, int b){
return a<b;
}
bool pair(int a, int b){
return true;
return a%2;
}
bool impair(int a, int b){
return true;
return !a%2;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment