Добавил к седьмому заданию сравнение разных сортировок, сделал 9е задание на массивы

This commit is contained in:
AZEN-SGG 2024-12-04 13:25:05 +03:00
parent c33f7295d5
commit a45af76154
25 changed files with 599 additions and 23 deletions

View file

@ -1,27 +1,35 @@
#include <stdio.h>
#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);
}

BIN
Sorting/7Ex/main.o Normal file

Binary file not shown.

View file

@ -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

View file

@ -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;
}
}

View file

@ -3,7 +3,9 @@
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
void quicksort(double * array, int length);
void bubble(double *arr, int length);
#endif

BIN
Sorting/7Ex/quicksort.o Normal file

Binary file not shown.

37
Sorting/7Ex/seagwithsob.c Normal file
View file

@ -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];
}

12
Sorting/7Ex/seagwithsob.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef SEAGWITHSOB
#define SEAGWITHSOB
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void sort(double * array, int length);
void rewrite(double * array, double * zero, int len_zero, double * unit, int len_unit);
#endif

BIN
Sorting/7Ex/seagwithsob.o Normal file

Binary file not shown.

View file

@ -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;
}
}

View file

@ -5,6 +5,7 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#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

View file

@ -0,0 +1 @@
0 -1 -2 -2.001 6 6 5 5 4 4 3 3 2 2 1 1

View file

@ -0,0 +1,27 @@
#include <stdio.h>
#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;
}

View file

@ -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

View file

@ -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);
}

View file

@ -0,0 +1,9 @@
#ifndef QUICKSORT
#define QUICKSORT
#include <stdlib.h>
#include <time.h>
void quicksort(double * array, int length);
#endif

108
Sorting/7ExOriginal/tools.c Normal file
View file

@ -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", &current) != 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", &current) == 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;
}

View file

@ -0,0 +1,24 @@
#ifndef TOOLS
#define TOOLS
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#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

View file

@ -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");
}

View file

@ -0,0 +1,14 @@
#ifndef GROUPING
#define GROUPING
#include <stdio.h>
#include <math.h>
#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

View file

@ -0,0 +1 @@
-1 -2 -3 -4 -5 0 1 2 3 4 5

27
WorkingArrays/9Ex/main.c Normal file
View file

@ -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;
}

View file

@ -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

47
WorkingArrays/9Ex/tools.c Normal file
View file

@ -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", &current) != 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", &current) == 1);
array = (double *)realloc(array, sizeof(double) * i);
array[0] = (double)i;
return array;
}

10
WorkingArrays/9Ex/tools.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef TOOLS
#define TOOLS
#include <stdio.h>
#include <stdlib.h>
FILE * getFile(void);
double * getArray(FILE * file);
#endif