From e5191ecddbd9a2a01e23893061512c052a6ec4bf Mon Sep 17 00:00:00 2001 From: thib <tempo2riz@gmail.com> Date: Wed, 9 Dec 2020 14:11:22 +0100 Subject: [PATCH] mesure time --- TP8-sort/Makefile | 9 +++++++++ TP8-sort/buble_sort.c | 10 +++++----- TP8-sort/main.c | 39 ++++++++++++++++++++++++++++----------- TP8-sort/quick_sort.c | 4 ++-- TP8-sort/stack_sort.c | 4 ++-- 5 files changed, 46 insertions(+), 20 deletions(-) diff --git a/TP8-sort/Makefile b/TP8-sort/Makefile index 50e109a..e473248 100644 --- a/TP8-sort/Makefile +++ b/TP8-sort/Makefile @@ -19,6 +19,15 @@ stack.x: stack_sort.o main.o stack_sort.o: stack_sort.c sort.h $(cc) -c $^ +# insert.x: insert_sort.o main.o +# $(cc) -o $@ $^ +# insert_sort.o: insert_sort.c sort.h +# $(cc) -c $^ + +# select.x: select_sort.o main.o +# $(cc) -o $@ $^ +# select_sort.o: select_sort.c sort.h +# $(cc) -c $^ main.o: main.c $(cc) -c $< diff --git a/TP8-sort/buble_sort.c b/TP8-sort/buble_sort.c index a1d61c6..773cd5d 100644 --- a/TP8-sort/buble_sort.c +++ b/TP8-sort/buble_sort.c @@ -5,15 +5,14 @@ bool isSorted(int size, int tab[size], bool (*comp)(int, int)){ for( int i = 1; i<size;i++){ if(comp(tab[i-1],tab[i])){ - printf("not sorted\n"); + //printf("not sorted\n"); return false; } } - printf("sorted !\n"); + //printf("sorted !\n"); return true; } - void random_tab(int size,int tab[size],int min,int max) { assert(max > min); for (int i=0;i<size;i++) { @@ -46,12 +45,13 @@ void sort(int *sorted, const int *const orig, int nitems, bool (*comp)(int, int) sorted[i] = orig[i]; } - for (int i = nitems; i > 1; i--) { - for (int i = 0; i < nitems - 1; i++) { + for (int j = nitems; j > 1; j--) { + for (int i = 0; i < j; i++) { if (comp(sorted[i], sorted[i + 1])) { swap(&sorted[i], &sorted[i + 1]); } } } + } \ No newline at end of file diff --git a/TP8-sort/main.c b/TP8-sort/main.c index 7e83f83..286419c 100644 --- a/TP8-sort/main.c +++ b/TP8-sort/main.c @@ -1,23 +1,40 @@ +#include "sort.h" #include <math.h> #include <stdio.h> #include <stdlib.h> -#include "sort.h" +#include <time.h> + + +int main() { -int main(){ + srand(time(NULL)); + int size = 1000; + int orig[size]; + int sorted[size]; + struct timespec start, finish; + clock_gettime(CLOCK_MONOTONIC, &start); + + // code à mesurer + for (int i = 0; i < 1000; i++) { + random_tab(size, orig, 1, 500); -int size=1000; -int orig[size]; -int sorted[size]; + sort(sorted, orig, size, isGreater); + // assert(isSorted(size, sorted, isSmaller)); + -random_tab(size, orig, 1, 500); + // sort(sorted, orig, size, isSmaller); + // assert(isSorted(size, sorted, isGreater)); + } -sort(sorted, orig, size, isSmaller); + clock_gettime(CLOCK_MONOTONIC, &finish); + double seconds_elapsed = finish.tv_sec - start.tv_sec; + seconds_elapsed += (finish.tv_nsec - start.tv_nsec) / 1.0e9; + printf("time elapsed : %f sec\n", seconds_elapsed); -isSorted(size, sorted, isSmaller); -print_array(orig,size); -print_array(sorted,size); + // print_array(orig,size); + // print_array(sorted,size); - return EXIT_SUCCESS; + return EXIT_SUCCESS; } diff --git a/TP8-sort/quick_sort.c b/TP8-sort/quick_sort.c index 48a5b59..281a3d3 100644 --- a/TP8-sort/quick_sort.c +++ b/TP8-sort/quick_sort.c @@ -4,11 +4,11 @@ bool isSorted(int size, int tab[size], bool (*comp)(int, int)){ for( int i = 1; i<size;i++){ if(comp(tab[i-1],tab[i])){ - printf("not sorted\n"); + //printf("not sorted\n"); return false; } } - printf("sorted !\n"); + //printf("sorted !\n"); return true; } diff --git a/TP8-sort/stack_sort.c b/TP8-sort/stack_sort.c index cd8c5da..f83234b 100644 --- a/TP8-sort/stack_sort.c +++ b/TP8-sort/stack_sort.c @@ -13,11 +13,11 @@ bool isSorted(int size, int tab[size], bool (*comp)(int, int)) { for (int i = 1; i < size; i++) { if (comp(tab[i - 1], tab[i])) { - printf("not sorted\n"); + // printf("not sorted\n"); return false; } } - printf("sorted !\n"); + //printf("sorted !\n"); return true; } -- GitLab