Написал 7е задание на быструю сортировку
This commit is contained in:
parent
c6539e2868
commit
c33f7295d5
10 changed files with 280 additions and 22 deletions
1
Sorting/7Ex/input.txt
Normal file
1
Sorting/7Ex/input.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
0 -1 -2 -2.001 6 6 5 5 4 4 3 3 2 2 1 1
|
27
Sorting/7Ex/main.c
Normal file
27
Sorting/7Ex/main.c
Normal 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;
|
||||||
|
}
|
43
Sorting/7Ex/makefile
Normal file
43
Sorting/7Ex/makefile
Normal 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
|
26
Sorting/7Ex/quicksort.c
Normal file
26
Sorting/7Ex/quicksort.c
Normal 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);
|
||||||
|
}
|
9
Sorting/7Ex/quicksort.h
Normal file
9
Sorting/7Ex/quicksort.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef QUICKSORT
|
||||||
|
#define QUICKSORT
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
void quicksort(double * array, int length);
|
||||||
|
|
||||||
|
#endif
|
108
Sorting/7Ex/tools.c
Normal file
108
Sorting/7Ex/tools.c
Normal 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", ¤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;
|
||||||
|
}
|
24
Sorting/7Ex/tools.h
Normal file
24
Sorting/7Ex/tools.h
Normal 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
|
|
@ -16,14 +16,12 @@ int main(void) {
|
||||||
|
|
||||||
orig_arr = array;
|
orig_arr = array;
|
||||||
array = &array[1];
|
array = &array[1];
|
||||||
printArray(array, length);
|
testSort(array, length, sort);
|
||||||
sort(array, length);
|
|
||||||
printArray(array, length);
|
|
||||||
|
|
||||||
free(orig_arr);
|
free(orig_arr);
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
randomArray();
|
testSortRandomArray(sort);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,18 +52,14 @@ bool orderliness(double * array, int length) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void randomArray(void) {
|
void testSortRandomArray(sort_op op) {
|
||||||
int length;
|
int length;
|
||||||
|
|
||||||
printf("Enter length of array: ");
|
printf("Enter length of array: ");
|
||||||
if (scanf("%d", &length) == 1) {
|
if (scanf("%d", &length) == 1) {
|
||||||
double * array = (double *)malloc(length * sizeof(double));
|
double * array = (double *)malloc(length * sizeof(double));
|
||||||
generate(array, length);
|
generate(array, length);
|
||||||
printArray(array, length);
|
testSort(array, length, op);
|
||||||
printf("\n");
|
|
||||||
sort(array, length);
|
|
||||||
printf("\n");
|
|
||||||
printArray(array, length);
|
|
||||||
free(array);
|
free(array);
|
||||||
} else {
|
} else {
|
||||||
printf("Length not entered!\n");
|
printf("Length not entered!\n");
|
||||||
|
@ -74,10 +70,24 @@ void generate(double * array, int length) {
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
for (int i = 0; i < length; ++i) {
|
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) {
|
void printArray(double * array, int length) {
|
||||||
length = (length > 10) ? 10 : length;
|
length = (length > 10) ? 10 : length;
|
||||||
|
|
||||||
|
@ -86,7 +96,15 @@ void printArray(double * array, int length) {
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n");
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,24 @@
|
||||||
#ifndef TOOLS
|
#ifndef TOOLS
|
||||||
#define TOOLS
|
#define TOOLS
|
||||||
|
|
||||||
#include "seagwithsob.h"
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#define exp -1.e-6
|
#define exp 1.e-6
|
||||||
|
|
||||||
|
typedef void (sort_op)(double * array, int length);
|
||||||
|
|
||||||
FILE * getFile(void);
|
FILE * getFile(void);
|
||||||
double * getArray(FILE * file);
|
double * getArray(FILE * file);
|
||||||
bool orderliness(double * array, int length);
|
bool orderliness(double * array, int length);
|
||||||
void randomArray(void);
|
void testSortRandomArray(sort_op op);
|
||||||
void generate(double * array, int length);
|
void generate(double * array, int length);
|
||||||
void printArray(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
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue