Добавил к седьмому заданию сравнение разных сортировок, сделал 9е задание на массивы
This commit is contained in:
parent
c33f7295d5
commit
a45af76154
25 changed files with 599 additions and 23 deletions
|
@ -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
BIN
Sorting/7Ex/main.o
Normal file
Binary file not shown.
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
BIN
Sorting/7Ex/quicksort.o
Normal file
Binary file not shown.
37
Sorting/7Ex/seagwithsob.c
Normal file
37
Sorting/7Ex/seagwithsob.c
Normal 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
12
Sorting/7Ex/seagwithsob.h
Normal 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
BIN
Sorting/7Ex/seagwithsob.o
Normal file
Binary file not shown.
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue