Написал 7е задание на быструю сортировку
This commit is contained in:
parent
c6539e2868
commit
c33f7295d5
10 changed files with 280 additions and 22 deletions
|
@ -3,27 +3,25 @@
|
|||
#include "tools.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
int main(void) {
|
||||
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);
|
||||
sort(array, length);
|
||||
printArray(array, length);
|
||||
|
||||
testSort(array, length, sort);
|
||||
|
||||
free(orig_arr);
|
||||
|
||||
printf("\n");
|
||||
randomArray();
|
||||
|
||||
testSortRandomArray(sort);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ double * getArray(FILE * file) {
|
|||
}
|
||||
|
||||
array = (double *)malloc(size * sizeof(double));
|
||||
|
||||
|
||||
i = 1;
|
||||
do {
|
||||
if (i >= size) {
|
||||
|
@ -42,7 +42,7 @@ double * getArray(FILE * file) {
|
|||
|
||||
array = (double *)realloc(array, sizeof(double) * i);
|
||||
array[0] = (double)i;
|
||||
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
|
@ -52,18 +52,14 @@ bool orderliness(double * array, int length) {
|
|||
return true;
|
||||
}
|
||||
|
||||
void randomArray(void) {
|
||||
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);
|
||||
printArray(array, length);
|
||||
printf("\n");
|
||||
sort(array, length);
|
||||
printf("\n");
|
||||
printArray(array, length);
|
||||
testSort(array, length, op);
|
||||
free(array);
|
||||
} else {
|
||||
printf("Length not entered!\n");
|
||||
|
@ -74,19 +70,41 @@ void generate(double * array, int length) {
|
|||
srand(time(NULL));
|
||||
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,20 +1,24 @@
|
|||
#ifndef TOOLS
|
||||
#define TOOLS
|
||||
|
||||
#include "seagwithsob.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.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);
|
||||
double * getArray(FILE * file);
|
||||
bool orderliness(double * array, int length);
|
||||
void randomArray(void);
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue