diff --git a/TP8-sort/Makefile b/TP8-sort/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..50e109a835181ee944769251a273b5367d7d4796 --- /dev/null +++ b/TP8-sort/Makefile @@ -0,0 +1,27 @@ +cc=gcc +LIBS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak -lm + +buble.x: buble_sort.o main.o + $(cc) -o $@ $^ +buble_sort.o: buble_sort.c sort.h + $(cc) -c $^ + + +quick.x: quick_sort.o main.o + $(cc) -o $@ $^ + +quick_sort.o: quick_sort.c sort.h + $(cc) -c $^ + + +stack.x: stack_sort.o main.o + $(cc) -o $@ $^ +stack_sort.o: stack_sort.c sort.h + $(cc) -c $^ + + +main.o: main.c + $(cc) -c $< + +clean: + rm -f *.o *.x diff --git a/TP8-sort/buble_sort.c b/TP8-sort/buble_sort.c new file mode 100644 index 0000000000000000000000000000000000000000..a1d61c6f387c7c1c0406dbd8c201ef900a569b87 --- /dev/null +++ b/TP8-sort/buble_sort.c @@ -0,0 +1,57 @@ +#include "sort.h" +#include <stdio.h> + +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"); + return false; + } + } + 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++) { + tab[i] = min+rand()%(max-min); + } +} + +bool isGreater(int a, int b) { return a > b; } + +bool isSmaller(int a, int b) { return a < b; } + +void swap(int *a, int *b) { + int tmp = *a; + *a = *b; + *b = tmp; +} + +void print_array(int *arr, int size) { + for (int i = 0; i < size; i++) { + printf("%d ", arr[i]); + } + printf("\n"); +} + + +void sort(int *sorted, const int *const orig, int nitems, bool (*comp)(int, int)) { + + // copy + for (int i = 0; i < nitems; i++) { + sorted[i] = orig[i]; + } + + for (int i = nitems; i > 1; i--) { + for (int i = 0; i < nitems - 1; 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 new file mode 100644 index 0000000000000000000000000000000000000000..7e83f834d6e05a1fb364edaba32d97291783f29b --- /dev/null +++ b/TP8-sort/main.c @@ -0,0 +1,23 @@ +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include "sort.h" + +int main(){ + + + +int size=1000; +int orig[size]; +int sorted[size]; + +random_tab(size, orig, 1, 500); + +sort(sorted, orig, size, isSmaller); + +isSorted(size, sorted, isSmaller); +print_array(orig,size); +print_array(sorted,size); + + return EXIT_SUCCESS; +} diff --git a/TP8-sort/quick_sort.c b/TP8-sort/quick_sort.c new file mode 100644 index 0000000000000000000000000000000000000000..48a5b597a7ae5f7bdda102deda1451915caf5871 --- /dev/null +++ b/TP8-sort/quick_sort.c @@ -0,0 +1,88 @@ +#include "sort.h" + +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"); + return false; + } + } + 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++) { + tab[i] = min+rand()%(max-min); + } +} + +bool isGreater(int a, int b) { return a > b; } + +bool isSmaller(int a, int b) { return a < b; } + +void swap(int *a, int *b) { + int tmp = *a; + *a = *b; + *b = tmp; +} + +void print_array(int *arr, int size) { + for (int i = 0; i < size; i++) { + printf("%d ", arr[i]); + } + printf("\n"); +} + + +// Partition du tableau <array> autour d'une valeur pivot: +int partition(int size, int array[size], int first, int last,bool (*comp)(int, int)) { + int pivot = array[last]; + int i = first; + int j = last-1; + while (true) { + + while (comp(array[i],pivot)){ + i++; + } //array[i]<pivot + + while (!comp(array[j],pivot)){ + j--; + } //array[j]>pivot + + if (i >= j) { + break; + } + // échanger cases <i> et <j> du tableau <array> + swap(&array[i], &array[j]); + } + // échanger cases <i> et <last> du tableau <array> + swap(&array[i], &array[last]); + return i; +} + +// Tri rapide récursif +void quicksort(int size, int array[size], int first, int last,bool (*comp)(int, int)) { + if (first < last) { + int midpoint = partition(size, array, first, last,comp); + if (first < midpoint - 1) { + quicksort(size, array, first, midpoint - 1,comp); + } + if (midpoint + 1 < last) { + quicksort(size, array, midpoint + 1, last,comp); + } + } +} + +void sort(int *sorted, const int *const orig, int nitems, bool (*comp)(int, int)) { + + // copy + for (int i = 0; i < nitems; i++) { + sorted[i] = orig[i]; + } + quicksort(nitems, sorted, 0, nitems-1,comp); + + +} diff --git a/TP8-sort/sort.h b/TP8-sort/sort.h new file mode 100644 index 0000000000000000000000000000000000000000..77c22662576b76d7b6eddd6439f6a9315cd0e4a9 --- /dev/null +++ b/TP8-sort/sort.h @@ -0,0 +1,28 @@ +#ifndef _SORT_H_ +#define _SORT_H_ + +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <assert.h> + +bool isSorted(int size, int tab[size], bool (*comp)(int, int)); + +void random_tab(int size,int tab[size],int min,int max); + +bool isGreater(int a, int b); + +bool isSmaller(int a, int b); + +void swap(int *a, int *b); + +void print_array(int *arr, int size); + + +void sort(int *sorted, const int *const orig, int nitems,bool (*comp)(int, int)); + + + + +#endif \ No newline at end of file diff --git a/TP8-sort/stack_sort.c b/TP8-sort/stack_sort.c new file mode 100644 index 0000000000000000000000000000000000000000..cd8c5da0623b40aded04412ced88d90be8eb0410 --- /dev/null +++ b/TP8-sort/stack_sort.c @@ -0,0 +1,116 @@ +#include "sort.h" +#include <stdlib.h> + +typedef struct _stack { + + int *data; + int top; + int capacity; + +} stack; + +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"); + return false; + } + } + 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++) { + tab[i] = min + rand() % (max - min); + } +} + +bool isStackEmpty(stack stack) { return stack.top == -1; } + +bool isStackFull(stack stack) { return stack.capacity - 1 == stack.top; } + +int stack_top(stack stack) { return stack.data[stack.top]; } + +int stack_count(stack stack) { return stack.top + 1; } + +void stack_init(stack *stack, int max) { + stack->data = malloc(sizeof(int) * max); + stack->top = -1; + stack->capacity = max; +} + +void stack_destroy(stack *stack) { + free(stack->data); + stack->top = -1; + stack->capacity = -1; +} + +void stack_add(stack *stack, int item) { + + if (!isStackFull(*stack)) { + stack->top++; + stack->data[stack->top] = item; + } +} + +int stack_remove(stack *stack) { + int tmp; + tmp = stack->data[stack->top]; + stack->top--; + return tmp; +} + +bool isGreater(int a, int b) { return a > b; } + +bool isSmaller(int a, int b) { return a < b; } + +void swap(int *a, int *b) { + int tmp = *a; + *a = *b; + *b = tmp; +} + +void print_array(int *arr, int size) { + for (int i = 0; i < size; i++) { + printf("%d ", arr[i]); + } + printf("\n"); +} + +void transfer(stack *a, stack *b) { stack_add(b, stack_remove(a)); } + +void sort(int *sorted, const int *const orig, int nitems, bool (*comp)(int, int)) { + stack g, d; + stack_init(&g, nitems); + stack_init(&d, nitems); + stack_add(&g, orig[0]); + + for (int i = 1; i < nitems; i++) { + + // transfère les éléments de <pile_g> à <pile_d> // tant que sommet(pile_g) + // < tab[i] + while (comp(stack_top(g), orig[i]) && !isStackEmpty(g)) { //stack_top(g) < orig[i] + transfer(&g, &d); + } + while (!comp(stack_top(d) , orig[i]) && !isStackEmpty(d)) { //stack_top(d) >= orig[i] + + transfer(&d, &g); + } + // empile l'élément dans <pile_g> + stack_add(&g, orig[i]); + } + + while (!isStackEmpty(d)) { + transfer(&d, &g); + } + + for (int i = 0; i < nitems; i++) { + sorted[i] = stack_remove(&g); + } + + stack_destroy(&d); + stack_destroy(&g); +} \ No newline at end of file