Добавил к седьмому заданию сравнение разных сортировок, сделал 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

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