diff --git a/TP2/tableau/main b/TP2/tableau/main index 96ba549..7e15e05 100755 Binary files a/TP2/tableau/main and b/TP2/tableau/main differ diff --git a/TP2/tableau/main.c b/TP2/tableau/main.c index 0d11104..10b1bbc 100644 --- a/TP2/tableau/main.c +++ b/TP2/tableau/main.c @@ -1,11 +1,41 @@ #include #include #include +#include + +#define NBVAL 1E4 +#define MIN 0 +#define MAX 1E5 + +clock_t start, finish; +double duration; int *InitTab(unsigned int nbVal, int min, int max); bool Verif(int *t, unsigned int nbVal); -void MonTri(int *t, unsigned int nbVal); -void quickSort(int *t, int left, int right); +void bubbleSort(int *t, unsigned int nbVal, int (*compare)(int, int)); +void quickSort(int *t, unsigned int nbVal, int (*compare)(int, int)); +void _quickSort(int *t, int left, int right, int (*compare)(int, int)); +void sort(int *t, unsigned int nbVal, void (*sortMethod)(int *t, unsigned int nbVal, int (*compare)(int, int)), int (*compare)(int, int)) +{ + sortMethod(t, nbVal, compare); +} + +int min2max(int a, int b) +{ + return a - b; +} +int max2min(int a, int b) +{ + return b - a; +} +int pair(int a, int b) +{ + if (a % 2 == 0 && b % 2 != 0) + return -1; + if (a % 2 != 0 && b % 2 == 0) + return 1; + return a - b; +} // if true print "true" // if false print "false" @@ -21,13 +51,48 @@ void printBool(bool b) } } +int *duplicate(int *t, unsigned int nbVal) +{ + int *t2 = (int *)malloc(nbVal * sizeof(int)); + for (int i = 0; i < nbVal; i++) + { + t2[i] = t[i]; + } + return t2; +} + +void printMethod(void (*sortMethod)(int *t, unsigned int nbVal, int (*compare)(int, int))) +{ + if (sortMethod == bubbleSort) + { + printf("bubbleSort"); + } + else if (sortMethod == quickSort) + { + printf("quickSort"); + } +} + int main() { - int *t = InitTab(10, 0, 100); - printBool(Verif(t, 10)); - // MonTri(t, 10); - quickSort(t, 0, 9); - printBool(Verif(t, 10)); + void *sortMethodArray[2] = {bubbleSort, quickSort}; + + for (int i = 0; i < 2; i++) + { + srand(0); + int *t = InitTab(NBVAL, MIN, MAX); + printf("----------------------"); + printMethod(sortMethodArray[i]); + printf("---------------------\n"); + printBool(Verif(t, NBVAL)); + start = clock(); + sort(t, NBVAL, sortMethodArray[i], min2max); + finish = clock(); + printBool(Verif(t, NBVAL)); + duration = (double)(finish - start) / CLOCKS_PER_SEC; + printf("%f seconds\n", duration); + printf("----------------------------------------------------\n"); + } return 0; } @@ -55,45 +120,60 @@ bool Verif(int *t, unsigned int nbVal) } } - for (int i = 0; i < nbVal; i++) + // print array 10 head and 10 tail + for (int i = 0; i < 10; i++) { printf("%d ", t[i]); } + printf("....... "); + for (int i = nbVal - 10; i < nbVal; i++) + { + printf("%d ", t[i]); + } + return flg; } -void MonTri(int *t, unsigned int nbVal) +void bubbleSort(int *t, unsigned int nbVal, int (*compare)(int, int)) { - int i, j, tmp; - for (i = 0; i < nbVal; i++) + int tmp; + for (int i = 0; i < nbVal - 1; i++) { - for (j = i + 1; j < nbVal; j++) + for (int j = 0; j < nbVal - 1 - i; j++) { - if (t[i] > t[j]) + if (compare(t[j], t[j + 1]) > 0) { - tmp = t[i]; - t[i] = t[j]; - t[j] = tmp; + tmp = t[j]; + t[j] = t[j + 1]; + t[j + 1] = tmp; } } } } // quick sort -void quickSort(int *t, int left, int right) +void quickSort(int *t, unsigned int size, int (*compare)(int, int)) { - int i = left, j = right; + _quickSort(t, 0, size - 1, compare); +} + +void _quickSort(int *t, int left, int right, int (*compare)(int, int)) +{ + if (right - left < 1) + return; + + int i = left + 1, j = right; int tmp; - int pivot = t[(left + right) / 2]; + int pivot = t[left]; /* partition */ while (i <= j) { - while (t[i] < pivot) + if (compare(t[i], pivot) < 0) i++; - while (t[j] > pivot) + else if (compare(t[j], pivot) >= 0) j--; - if (i <= j) + else { tmp = t[i]; t[i] = t[j]; @@ -101,11 +181,13 @@ void quickSort(int *t, int left, int right) i++; j--; } - }; + } + tmp = t[left]; + t[left] = t[j]; + t[j] = tmp; /* recursion */ - if (left < j) - quickSort(t, left, j); - if (i < right) - quickSort(t, i, right); -} \ No newline at end of file + + _quickSort(t, left, j - 1, compare); + _quickSort(t, j + 1, right, compare); +} diff --git a/file_operate/myls/main.c b/file_operate/myls/main.c new file mode 100644 index 0000000..e69de29