diff --git a/Sorting/7Ex/main.c b/Sorting/7Ex/main.c index 9b9d54a..92fa96b 100644 --- a/Sorting/7Ex/main.c +++ b/Sorting/7Ex/main.c @@ -1,27 +1,35 @@ #include #include "quicksort.h" +#include "seagwithsob.h" #include "tools.h" #include "stdlib.h" +void iquicksort(double * array, int length); + int main(void) { - FILE * file = getFile(); - double * array; - double * orig_arr; - int length; +// 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); - 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); +// printf("\n"); +// testSortRandomArray(quicksort; + compareSorts(quicksort, iquicksort); return 0; } + +void iquicksort(double * array, int length) { + qsort(array, length, sizeof(double), compare); +} diff --git a/Sorting/7Ex/main.o b/Sorting/7Ex/main.o new file mode 100644 index 0000000..c6ebb1f Binary files /dev/null and b/Sorting/7Ex/main.o differ diff --git a/Sorting/7Ex/makefile b/Sorting/7Ex/makefile index 7199963..6e7d53b 100644 --- a/Sorting/7Ex/makefile +++ b/Sorting/7Ex/makefile @@ -29,8 +29,8 @@ CFLAGS = -mfpmath=sse \ -c -all: main.o quicksort.o tools.o - gcc main.o quicksort.o tools.o -lssp && del *.o +all: main.o quicksort.o seagwithsob.o tools.o + gcc main.o quicksort.o seagwithsob.o tools.o -lssp && del *.o a.exe main.o: main.c @@ -38,6 +38,9 @@ main.o: main.c quicksort.o: quicksort.c gcc $(CFLAGS) quicksort.c + +seagwithsob.o: seagwithsob.c + gcc $(CFLAGS) seagwithsob.c tools.o: tools.c gcc $(CFLAGS) tools.c diff --git a/Sorting/7Ex/quicksort.c b/Sorting/7Ex/quicksort.c index 807f0d3..932c80a 100644 --- a/Sorting/7Ex/quicksort.c +++ b/Sorting/7Ex/quicksort.c @@ -21,6 +21,32 @@ void quicksort(double * array, int length) { } } while (si < ei); - if (si > 1) quicksort(array, si); - if (ei < length - 2) quicksort(&array[head + 1], length - ei - 1); + if (si > 1) { + if (si < 7) bubble(array, si); + else quicksort(array, si); + } + if (ei < length - 2) { + if (si < 7) bubble(&array[head + 1], length - ei - 1); + else quicksort(&array[head + 1], length - ei - 1); + } +} + +void bubble(double *arr, int length) { + double temp; + bool swapped; + + for (int i = 0; i < length; ++i) { + swapped = false; + + for (int j = 0; j < length - i - 1; ++j) { + if (arr[j] > arr[j + 1]) { + swapped = true; + temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + + if (!swapped) break; + } } diff --git a/Sorting/7Ex/quicksort.h b/Sorting/7Ex/quicksort.h index 52ab6ff..571be41 100644 --- a/Sorting/7Ex/quicksort.h +++ b/Sorting/7Ex/quicksort.h @@ -3,7 +3,9 @@ #include #include +#include void quicksort(double * array, int length); +void bubble(double *arr, int length); #endif diff --git a/Sorting/7Ex/quicksort.o b/Sorting/7Ex/quicksort.o new file mode 100644 index 0000000..b59dd0b Binary files /dev/null and b/Sorting/7Ex/quicksort.o differ diff --git a/Sorting/7Ex/seagwithsob.c b/Sorting/7Ex/seagwithsob.c new file mode 100644 index 0000000..cebb397 --- /dev/null +++ b/Sorting/7Ex/seagwithsob.c @@ -0,0 +1,37 @@ +#include "seagwithsob.h" + +void sort(double * array, int length) { + double * zero = (double *)malloc(length * sizeof(double)); + double * unit = (double *)malloc(length * sizeof(double)); + + int index; + + for (int j = 0; j < 63; ++j) { + index = 0; + + for (int i = 0; i < length; ++i) { + if ((*(unsigned long long*)&array[i] >> j) & 1) unit[index++] = array[i]; + else zero[i - index] = array[i]; + } + + rewrite(array, zero, length - index, unit, index); + } + + index = 0; + + for (int i = 0; i < length; ++i) { + if ((*(unsigned long long*)&array[i] >> 63) & 1) unit[index++] = array[i]; + else zero[i - index] = array[i]; + } + + for (int i = 0; i < index; ++i) array[i] = unit[index - i - 1]; + for (int i = index; i < length; ++i) array[i] = zero[i - index]; + + free(zero); + free(unit); +} + +void rewrite(double * array, double * zero, int len_zero, double * unit, int len_unit) { + for (int i = 0; i < len_zero; ++i) array[i] = zero[i]; + for (int i = len_zero; i < len_zero + len_unit; ++i) array[i] = unit[i - len_zero]; +} diff --git a/Sorting/7Ex/seagwithsob.h b/Sorting/7Ex/seagwithsob.h new file mode 100644 index 0000000..e2f1ab5 --- /dev/null +++ b/Sorting/7Ex/seagwithsob.h @@ -0,0 +1,12 @@ +#ifndef SEAGWITHSOB +#define SEAGWITHSOB + +#include +#include +#include +#include + +void sort(double * array, int length); +void rewrite(double * array, double * zero, int len_zero, double * unit, int len_unit); + +#endif diff --git a/Sorting/7Ex/seagwithsob.o b/Sorting/7Ex/seagwithsob.o new file mode 100644 index 0000000..6e86ed7 Binary files /dev/null and b/Sorting/7Ex/seagwithsob.o differ diff --git a/Sorting/7Ex/tools.c b/Sorting/7Ex/tools.c index afa3644..7465f29 100644 --- a/Sorting/7Ex/tools.c +++ b/Sorting/7Ex/tools.c @@ -66,6 +66,33 @@ void testSortRandomArray(sort_op op) { } } +void compareSorts(sort_op first, sort_op second) { + int length; + + printf("Enter length of array: "); + if (scanf("%d", &length) == 1) { + double * first_arr = (double *)malloc(length * sizeof(double)); + double * second_arr = (double *)malloc(length * sizeof(double)); + int first_timer, second_timer, difference; + + + generate(first_arr, length); + copy(first_arr, second_arr, length); + + first_timer = testSort(first_arr, length, first); + printf("\n"); + second_timer = testSort(second_arr, length, second); + + difference = abs(first_timer - second_timer); + printf(first_timer < second_timer ? "first sort was faster on %lf sec.\n" : "second sort was faster on %lf sec.\n", (double)difference / CLOCKS_PER_SEC); + + free(first_arr); + free(second_arr); + } else { + printf("Length not entered!\n"); + } +} + void generate(double * array, int length) { srand(time(NULL)); @@ -74,7 +101,11 @@ void generate(double * array, int length) { } } -void testSort(double * array, int length, sort_op op) { +void copy(double * from, double * to, int length) { + for (int i = 0; i < length; ++i) to[i] = from[i]; +} + +int testSort(double * array, int length, sort_op op) { int timer; printArray(array, length); @@ -86,6 +117,8 @@ void testSort(double * array, int length, sort_op op) { 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"); + + return timer; } void printArray(double * array, int length) { @@ -106,3 +139,16 @@ bool isSorted(double * array, int length) { bool more(double a, double b) { return (a - b) > exp; } + +int compare(const void * ufirst, const void * usecond) { + double first = (double)ufirst; + double second = (double)usecond; + + if ((first - second) > eps) { + return 1; + } else ((second - first) > eps) { + return -1; + } else { + return 0; + } +} diff --git a/Sorting/7Ex/tools.h b/Sorting/7Ex/tools.h index 3fe14d0..ba4eb3e 100644 --- a/Sorting/7Ex/tools.h +++ b/Sorting/7Ex/tools.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #define exp 1.e-6 @@ -15,10 +16,13 @@ FILE * getFile(void); double * getArray(FILE * file); bool orderliness(double * array, int length); void testSortRandomArray(sort_op op); +void compareSorts(sort_op first, sort_op second); void generate(double * array, int length); +void copy(double * from, double * to, int length); void printArray(double * array, int length); -void testSort(double * array, int length, sort_op op); +int testSort(double * array, int length, sort_op op); bool isSorted(double * array, int length); bool more(double a, double b); +int compare(const void * ufirst, const void * usecond); #endif diff --git a/Sorting/7ExOriginal/input.txt b/Sorting/7ExOriginal/input.txt new file mode 100644 index 0000000..7cb5790 --- /dev/null +++ b/Sorting/7ExOriginal/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/7ExOriginal/main.c b/Sorting/7ExOriginal/main.c new file mode 100644 index 0000000..9b9d54a --- /dev/null +++ b/Sorting/7ExOriginal/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/7ExOriginal/makefile b/Sorting/7ExOriginal/makefile new file mode 100644 index 0000000..7199963 --- /dev/null +++ b/Sorting/7ExOriginal/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/7ExOriginal/quicksort.c b/Sorting/7ExOriginal/quicksort.c new file mode 100644 index 0000000..807f0d3 --- /dev/null +++ b/Sorting/7ExOriginal/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/7ExOriginal/quicksort.h b/Sorting/7ExOriginal/quicksort.h new file mode 100644 index 0000000..52ab6ff --- /dev/null +++ b/Sorting/7ExOriginal/quicksort.h @@ -0,0 +1,9 @@ +#ifndef QUICKSORT +#define QUICKSORT + +#include +#include + +void quicksort(double * array, int length); + +#endif diff --git a/Sorting/7ExOriginal/tools.c b/Sorting/7ExOriginal/tools.c new file mode 100644 index 0000000..afa3644 --- /dev/null +++ b/Sorting/7ExOriginal/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/7ExOriginal/tools.h b/Sorting/7ExOriginal/tools.h new file mode 100644 index 0000000..3fe14d0 --- /dev/null +++ b/Sorting/7ExOriginal/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/WorkingArrays/9Ex/grouping.c b/WorkingArrays/9Ex/grouping.c new file mode 100644 index 0000000..4f921f8 --- /dev/null +++ b/WorkingArrays/9Ex/grouping.c @@ -0,0 +1,58 @@ +#include "grouping.h" + +void group(double * array, int length) { + negativeToRight(array, length); + zeroesToCenter(array, length); +} + +void negativeToRight(double * array, int length) { + double temp; + int negative = 0, last_index = length - 1; + + for (int i = 0; i <= last_index - negative; ++i) { + // Так как мы передвигаем каждый отрицательный эл-нт в конец + // То тогда нам не нужно проверять последний эл-нт (т.к. он отрицательный) - поэтому я его пропускаю - -negative + if (array[i] < 0) { + for (int j = length - 2; j >= i; --j) { + // Сначала последний эл-нт копируется в предпоследний, а предпоследний в последний + // Далее каждый следующий j-й элемент заменяется тем, что находится на месте последнего, а последний элемент заменяется на j + // Так шаг за шагов элементы переставляются и наш i-й элемент оказывается на месте последнего (типа ЭП2) + temp = array[j]; + array[j] = array[last_index]; + array[last_index] = temp; + } + + --i, ++negative; + } + } +} + +void zeroesToCenter(double * array, int length) { + // Аналогичный способ перемещения эл-тов, но только не в конец, а в место между отрицательными и положительными числами + double temp; + int zeroes = 0, last_index = 0; + + while (array[last_index] >= 0 && last_index < length) ++last_index; + // last_index < length - очень важен, так как если нет отрицательных чисел, то цикл уёдет в бесконечность + --last_index; // Так как изначально указывает на первый отрицательный элемент, а нам нужен последний положительный + + for (int i = 0; i <= last_index - zeroes; ++i) { + if (fabs(array[i]) < eps) { + for (int j = last_index - 1; j >= i; --j) { + temp = array[j]; + array[j] = array[last_index]; + array[last_index] = temp; + } + + --i, ++zeroes; + } + } +} + + +void printArray(double * array, int length) { + for (int i = 0; i < length; ++i) { + printf("%.2lf ", array[i]); + } + printf("\n"); +} diff --git a/WorkingArrays/9Ex/grouping.h b/WorkingArrays/9Ex/grouping.h new file mode 100644 index 0000000..6e3bf52 --- /dev/null +++ b/WorkingArrays/9Ex/grouping.h @@ -0,0 +1,14 @@ +#ifndef GROUPING +#define GROUPING + +#include +#include + +#define eps 1.e-6 + +void group(double * array, int length); +void negativeToRight(double * array, int length); +void zeroesToCenter(double * array, int length); +void printArray(double * array, int length); + +#endif diff --git a/WorkingArrays/9Ex/input.txt b/WorkingArrays/9Ex/input.txt new file mode 100644 index 0000000..7e3460a --- /dev/null +++ b/WorkingArrays/9Ex/input.txt @@ -0,0 +1 @@ +-1 -2 -3 -4 -5 0 1 2 3 4 5 diff --git a/WorkingArrays/9Ex/main.c b/WorkingArrays/9Ex/main.c new file mode 100644 index 0000000..8949744 --- /dev/null +++ b/WorkingArrays/9Ex/main.c @@ -0,0 +1,27 @@ +#include "tools.h" +#include "grouping.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]; + + printArray(array, length); + + group(array, length); + + printArray(array, length); + + free(orig_arr); + + return 0; +} diff --git a/WorkingArrays/9Ex/makefile b/WorkingArrays/9Ex/makefile new file mode 100644 index 0000000..556c520 --- /dev/null +++ b/WorkingArrays/9Ex/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 grouping.o tools.o + gcc main.o grouping.o tools.o -lssp && del *.o + a.exe + +main.o: main.c + gcc $(CFLAGS) main.c + +grouping.o: grouping.c + gcc $(CFLAGS) grouping.c + +tools.o: tools.c + gcc $(CFLAGS) tools.c diff --git a/WorkingArrays/9Ex/tools.c b/WorkingArrays/9Ex/tools.c new file mode 100644 index 0000000..50b7a52 --- /dev/null +++ b/WorkingArrays/9Ex/tools.c @@ -0,0 +1,47 @@ +#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; +} diff --git a/WorkingArrays/9Ex/tools.h b/WorkingArrays/9Ex/tools.h new file mode 100644 index 0000000..9702590 --- /dev/null +++ b/WorkingArrays/9Ex/tools.h @@ -0,0 +1,10 @@ +#ifndef TOOLS +#define TOOLS + +#include +#include + +FILE * getFile(void); +double * getArray(FILE * file); + +#endif