diff --git a/TP8-sort/Makefile b/TP8-sort/Makefile index 50e109a835181ee944769251a273b5367d7d4796..e473248ac7000b6a089f22ac5cde5517decebb36 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 a1d61c6f387c7c1c0406dbd8c201ef900a569b87..773cd5d15f44b14ca8c0cea983e8debe2d3cfaa6 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 7e83f834d6e05a1fb364edaba32d97291783f29b..286419cdff64c2b3e0c0542778792a7c49702bcb 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 48a5b597a7ae5f7bdda102deda1451915caf5871..281a3d3dfce779ba8961926508db07f8d7158a71 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 cd8c5da0623b40aded04412ced88d90be8eb0410..f83234b719ca090d899e9aebebe0ce3d3c03f9f5 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; }