diff --git a/Sorting/7Ex/input.txt b/Sorting/7Ex/input.txt new file mode 100644 index 0000000..7cb5790 --- /dev/null +++ b/Sorting/7Ex/input.txt @@ -0,0 +1 @@ +0 -1 -2 -2.001 6 6 5 5 4 4 3 3 2 2 1 1 diff --git a/Sorting/7Ex/main.c b/Sorting/7Ex/main.c new file mode 100644 index 0000000..9b9d54a --- /dev/null +++ b/Sorting/7Ex/main.c @@ -0,0 +1,27 @@ +#include +#include "quicksort.h" +#include "tools.h" +#include "stdlib.h" + +int main(void) { + FILE * file = getFile(); + double * array; + double * orig_arr; + int length; + + if (file == NULL) return -1; + array = getArray(file); + if (array == NULL) return -2; + length = (int)array[0] - 1; + + orig_arr = array; + array = &array[1]; + testSort(array, length, quicksort); + + free(orig_arr); + + printf("\n"); + testSortRandomArray(quicksort); + + return 0; +} diff --git a/Sorting/7Ex/makefile b/Sorting/7Ex/makefile new file mode 100644 index 0000000..7199963 --- /dev/null +++ b/Sorting/7Ex/makefile @@ -0,0 +1,43 @@ +CFLAGS = -mfpmath=sse \ + -fstack-protector-all \ + -W \ + -Wall \ + -Wextra \ + -Wunused \ + -Wcast-align \ + -Werror \ + -pedantic \ + -pedantic-errors \ + -Wfloat-equal \ + -Wpointer-arith \ + -Wformat-security \ + -Wmissing-format-attribute \ + -Wformat=1 \ + -Wwrite-strings \ + -Wcast-align \ + -Wno-long-long \ + -std=gnu99 \ + -Wstrict-prototypes \ + -Wmissing-prototypes \ + -Wmissing-declarations \ + -Wold-style-definition \ + -Wdeclaration-after-statement \ + -Wbad-function-cast \ + -Wnested-externs \ + -O3 \ + -D_DEBUG -g \ + -c + + +all: main.o quicksort.o tools.o + gcc main.o quicksort.o tools.o -lssp && del *.o + a.exe + +main.o: main.c + gcc $(CFLAGS) main.c + +quicksort.o: quicksort.c + gcc $(CFLAGS) quicksort.c + +tools.o: tools.c + gcc $(CFLAGS) tools.c diff --git a/Sorting/7Ex/quicksort.c b/Sorting/7Ex/quicksort.c new file mode 100644 index 0000000..807f0d3 --- /dev/null +++ b/Sorting/7Ex/quicksort.c @@ -0,0 +1,26 @@ +#include "quicksort.h" + +void quicksort(double * array, int length) { + int head, si = 0, ei = length - 1; + double temp; + + srand(time(NULL)); + head = rand() % length; + + do { + while (array[si] < array[head]) ++si; + while (array[ei] > array[head]) --ei; + + if (si < ei) { + temp = array[si]; + array[si] = array[ei]; + array[ei] = temp; + + if (si == head || ei == head) head = si == head ? ei++ : si--; + ++si, --ei; + } + } while (si < ei); + + if (si > 1) quicksort(array, si); + if (ei < length - 2) quicksort(&array[head + 1], length - ei - 1); +} diff --git a/Sorting/7Ex/quicksort.h b/Sorting/7Ex/quicksort.h new file mode 100644 index 0000000..52ab6ff --- /dev/null +++ b/Sorting/7Ex/quicksort.h @@ -0,0 +1,9 @@ +#ifndef QUICKSORT +#define QUICKSORT + +#include +#include + +void quicksort(double * array, int length); + +#endif diff --git a/Sorting/7Ex/tools.c b/Sorting/7Ex/tools.c new file mode 100644 index 0000000..afa3644 --- /dev/null +++ b/Sorting/7Ex/tools.c @@ -0,0 +1,108 @@ +#include "tools.h" + +FILE * getFile(void) { + char filename[50]; + + printf("Enter file name: "); + if (scanf("%s", filename) == 1) { + FILE * file = fopen(filename, "r"); + if (file == NULL) { + printf("Error file!\n)"); + return NULL; + } else { + return file; + } + } else { + printf("Empty name!\n"); + return NULL; + } +} + +double * getArray(FILE * file) { + int i, size = 2; + double * array = NULL; + double current; + + if (fscanf(file, "%lf", ¤t) != 1) { + printf("File if empty!"); + return array; + } + + array = (double *)malloc(size * sizeof(double)); + + i = 1; + do { + if (i >= size) { + size *= 2; + array = (double *)realloc(array, sizeof(double) * size); + } + array[i] = current; + ++i; + } while (fscanf(file, "%lf", ¤t) == 1); + + array = (double *)realloc(array, sizeof(double) * i); + array[0] = (double)i; + + return array; +} + + +bool orderliness(double * array, int length) { + for (int i = 1; i < length; ++i) if ((array[i] - array[i - 1]) < exp) return false; + return true; +} + +void testSortRandomArray(sort_op op) { + int length; + + printf("Enter length of array: "); + if (scanf("%d", &length) == 1) { + double * array = (double *)malloc(length * sizeof(double)); + generate(array, length); + testSort(array, length, op); + free(array); + } else { + printf("Length not entered!\n"); + } +} + +void generate(double * array, int length) { + srand(time(NULL)); + + for (int i = 0; i < length; ++i) { + array[i] = ((rand() % 2) == 1 ? -1. : 1.) * 1.*rand() / (1. + rand()) * 1000; + } +} + +void testSort(double * array, int length, sort_op op) { + int timer; + + printArray(array, length); + + timer = -clock(); + op(array, length); + timer += clock(); + + printArray(array, length); + if (isSorted(array, length)) printf("The array was sorted for %lf sec.\n", (double)timer / CLOCKS_PER_SEC); + else printf("The array was not sorted!\n"); +} + +void printArray(double * array, int length) { + length = (length > 10) ? 10 : length; + + for (int i = 0; i < length; ++i) { + printf("%.2lf ", array[i]); + } + + printf("\n"); +} + +bool isSorted(double * array, int length) { + for (int i = 0; i < length - 1; ++i) if (more(array[i], array[i + 1])) return false; + return true; +} + +bool more(double a, double b) { + return (a - b) > exp; +} diff --git a/Sorting/7Ex/tools.h b/Sorting/7Ex/tools.h new file mode 100644 index 0000000..3fe14d0 --- /dev/null +++ b/Sorting/7Ex/tools.h @@ -0,0 +1,24 @@ +#ifndef TOOLS +#define TOOLS + +#include +#include +#include +#include +#include + +#define exp 1.e-6 + +typedef void (sort_op)(double * array, int length); + +FILE * getFile(void); +double * getArray(FILE * file); +bool orderliness(double * array, int length); +void testSortRandomArray(sort_op op); +void generate(double * array, int length); +void printArray(double * array, int length); +void testSort(double * array, int length, sort_op op); +bool isSorted(double * array, int length); +bool more(double a, double b); + +#endif diff --git a/Sorting/9Ex/main.c b/Sorting/9Ex/main.c index 8c7f078..80de71e 100644 --- a/Sorting/9Ex/main.c +++ b/Sorting/9Ex/main.c @@ -3,27 +3,25 @@ #include "tools.h" #include "stdlib.h" -int main(void) { +int main(void) { FILE * file = getFile(); double * array; double * orig_arr; int length; - + if (file == NULL) return -1; array = getArray(file); if (array == NULL) return -2; length = (int)array[0] - 1; - + orig_arr = array; array = &array[1]; - printArray(array, length); - sort(array, length); - printArray(array, length); - + testSort(array, length, sort); + free(orig_arr); printf("\n"); - randomArray(); - + testSortRandomArray(sort); + return 0; } diff --git a/Sorting/9Ex/tools.c b/Sorting/9Ex/tools.c index 723b801..8e534f4 100644 --- a/Sorting/9Ex/tools.c +++ b/Sorting/9Ex/tools.c @@ -29,7 +29,7 @@ double * getArray(FILE * file) { } array = (double *)malloc(size * sizeof(double)); - + i = 1; do { if (i >= size) { @@ -42,7 +42,7 @@ double * getArray(FILE * file) { array = (double *)realloc(array, sizeof(double) * i); array[0] = (double)i; - + return array; } @@ -52,18 +52,14 @@ bool orderliness(double * array, int length) { return true; } -void randomArray(void) { +void testSortRandomArray(sort_op op) { int length; printf("Enter length of array: "); if (scanf("%d", &length) == 1) { double * array = (double *)malloc(length * sizeof(double)); generate(array, length); - printArray(array, length); - printf("\n"); - sort(array, length); - printf("\n"); - printArray(array, length); + testSort(array, length, op); free(array); } else { printf("Length not entered!\n"); @@ -74,19 +70,41 @@ void generate(double * array, int length) { srand(time(NULL)); for (int i = 0; i < length; ++i) { - array[i] = 1.*rand() / (1. + rand()) * 1000; + array[i] = ((rand() % 2) == 1 ? -1. : 1.) * 1.*rand() / (1. + rand()) * 1000; } } +void testSort(double * array, int length, sort_op op) { + int timer; + + printArray(array, length); + + timer = -clock(); + op(array, length); + timer += clock(); + + printArray(array, length); + if (isSorted(array, length)) printf("The array was sorted for %lf sec.\n", (double)timer / CLOCKS_PER_SEC); + else printf("The array was not sorted!\n"); +} + void printArray(double * array, int length) { length = (length > 10) ? 10 : length; - + for (int i = 0; i < length; ++i) { printf("%.2lf ", array[i]); } printf("\n"); +} +bool isSorted(double * array, int length) { + for (int i = 0; i < length - 1; ++i) if (more(array[i], array[i + 1])) return false; + return true; +} + +bool more(double a, double b) { + return (a - b) > exp; } diff --git a/Sorting/9Ex/tools.h b/Sorting/9Ex/tools.h index bc80a94..3fe14d0 100644 --- a/Sorting/9Ex/tools.h +++ b/Sorting/9Ex/tools.h @@ -1,20 +1,24 @@ #ifndef TOOLS #define TOOLS -#include "seagwithsob.h" #include #include #include #include #include -#define exp -1.e-6 +#define exp 1.e-6 + +typedef void (sort_op)(double * array, int length); FILE * getFile(void); double * getArray(FILE * file); bool orderliness(double * array, int length); -void randomArray(void); +void testSortRandomArray(sort_op op); void generate(double * array, int length); void printArray(double * array, int length); +void testSort(double * array, int length, sort_op op); +bool isSorted(double * array, int length); +bool more(double a, double b); #endif