SOLVED: the answer was - the range of inputs was too small :) Thanks @sagi !
I've implemented a Quicksort algorithm for my classes and had to time the results for random data inputs (from -100 to 100).
Turns out, it follows N^2 time complexity almost to the dot and I have no idea why.
Here's the results of timing: Time results
My time calculating function.
void Timing (void(*f)(int*, int), int *tab, int n, char *wiadomosc) {
clock_t start = clock();
f(tab, n); //losowe dane
clock_t end = clock();
printf("%s %lf \n", wiadomosc, (double)(end - start) / (CLOCKS_PER_SEC));
}
void QUICK_SORT_F(int *tab, int n) { //pivot as last element
quick_sort(tab, 0, n-1);
}
void quick_sort(int *tab, int first_indx, int last_indx) { //pivot as last element
while (first_indx < last_indx) {
int P = Part(tab, first_indx, last_indx);
if (P - first_indx > last_indx - P) {
quick_sort(tab, first_indx, P-1);
first_indx = P + 1;
} else {
quick_sort(tab, P + 1, last_indx);
last_indx = P - 1;
}
}
}
int Part(int* tab, int minIndx, int maxIndx) {
int pivot = tab[maxIndx];
int P = minIndx - 1;
for (int i = minIndx; i < maxIndx; ++i) {
if (tab[i] < pivot) {
++P;
SWAP(tab, i, P);
}
}
SWAP(tab, P + 1, maxIndx);
return P + 1;
} //for quicksort
void SWAP(int *tab, int x, int y) { //swaps tab[x] with tab[y]
int hld = tab[x];
tab[x] = tab[y];
tab[y] = hld;
}
As for popular request, here's the code to generate data:
int generuj(int min, int max) {
return (rand() % (max - min) + min);
}
void TESTY(void(*f)(int*, int),int n, char* metoda) {
FILE* FWB = fopen(PLIK_Z_DANYMI, "wb");
for (int i = 0; i < n; ++i) {
int r = generuj(-100, 100);
fwrite(&r, sizeof(r), 1, FWB);
}
fclose(FWB);
testowanko(f, n,metoda);
}
void testowanko(void(*f)(int*, int), int n, char* metoda) {
printf("\t %s \t \n",metoda);
FILE* RF = fopen(PLIK_Z_DANYMI, "rb");
int* tab = malloc(sizeof(int) * n);
fread(tab, sizeof(int), n, RF);
Timowanie(f, tab, n, "czas sortowania przy losowych danych = ");
}