Muszę napisać funkcję sprawdzająca w jakim czasie algorytm sortowania radzi sobie z sortowaniem danych.
Wymyśliłem coś takiego, niestety mam pewien problem z poniższym kodem. Program po prostu się wysypuje, dostaję komunikat w debugerze SIGSEGV:Segmentational fault.
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include "SortTest.h"
int randN(int n) {
return rand() % n;
}
void initTestArray(int tab[], int n, int dlugoscPerm) {
for (int i = 0; i < n; i++) {
tab[i] = randN(dlugoscPerm);
}
}
void howFast(void (*sort)(int*, int),const int ileTestow,const int ileLiczb,
const int dlugoscLiczb, double wynik[3]) {
double czasy[ileTestow];
int liczby[ileLiczb];
double max = 0, min = 10000000, a, b;
for (int i = 0; i < ileTestow; i++) {
initTestArray(liczby, ileLiczb, dlugoscLiczb);
a = clock();
sort(liczby, ileLiczb);
b = clock() - a;
czasy[i] = b;
if (b > max)
max = b;
else if (b < min)
min = b;
}
double sredniCzas = 0;
for (int i = 0; i < ileTestow; i++) {
sredniCzas += czasy[i];
}
wynik[0] = min;
wynik[1] = sredniCzas / ileTestow;
wynik[2] = max;
}
void howFastQ(
void (*sort)(void *, size_t, size_t,
int (*comparator)(const void *, const void *)),
const int ileTestow, const int ileLiczb, const int dlugoscLiczb,
double wynik[]) {
int liczby[ileLiczb];
double czasy[ileTestow];
double max = 0.0, min = 10000000, a, b;
for (int i = 0; i < ileTestow; i++) {
initTestArray(liczby, ileLiczb, dlugoscLiczb);
a = clock();
sort(liczby, ileLiczb, sizeof(double), cmpfunc);
b = clock() - a;
czasy[i] = b;
if (b > max)
max = b;
else if (b < min)
min = b;
}
double sredniCzas = 0;
for (int i = 0; i < ileTestow; i++) {
sredniCzas += czasy[i];
}
wynik[0] = min;
wynik[1] = sredniCzas / ileTestow;
wynik[2] = max;
}
int cmpfunc(const void * a, const void * b) {
return (*(int*) a - *(int*) b);
}
bool isSorted(double tab[], double sortedTab[], int n) {
quickSort(tab, 0, n);
for (int i = 0; i < n; i++) {
if (tab[i] != sortedTab[i]) {
return false;
}
}
return true;
}
#include <stdbool.h>
#include <stdlib.h>
#ifndef SORTTEST_H_
#define SORTTEST_H_
void initTestArray(int tab[], int n, int dlugoscPerm);
void howFast(void (*sort)(int*, int), const int ileTestow, const int ileLiczb,
const int dlugoscLiczb, double wynik[3]);
void howFastQ(
void (*sort)(void *, size_t, size_t,
int (*comparator)(const void *, const void *)),
const int ileTestow, const int ileLiczb, const int dlugoscLiczb,
double wynik[]);
bool isSorted(double tab[], double sortedTab[], int n);
int randN(int n);
int cmpfunc(const void * a, const void * b);
#endif /* SORTTEST_H_ */
#include <stdio.h>
#include <stdlib.h>
#include "SortTest.h"
void sort(int tab[], int n);
int main(void) {
double tab[3];
howFastQ(qsort, 100, 5000, 1000, tab);
printf("%f\n", tab[0]);
printf("%f\n", tab[1]);
printf("%f\n", tab[2]);
howFast(sort, 100, 5000, 1000, tab);
printf("%f\n", tab[0]);
printf("%f\n", tab[1]);
printf("%f\n", tab[2]);
return EXIT_SUCCESS;
}
void sort(int tab[], int n) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (tab[i] < tab[j]) {
int a = tab[i];
tab[i] = tab[j];
tab[j] = a;
}
}
}
}
EDIT: Drobne zmiany w kodzie...
EDIT 2: Błędem było sizeof(double) przy tablicy int..... solved :D