1

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 = ");
}
Jedrzej
  • 7
  • 2
  • The complexity of quicksort is n^2 for the worst case https://stackoverflow.com/questions/4019528/quick-sort-worst-case – Michael Kotzjan May 20 '21 at 10:00
  • How many elements are in the array? if it's a big number, if you try a larger range of data, is it still close to n^2 ? – sagi May 20 '21 at 10:01
  • Could there be a bug in the way you generate the random data.... you didn't post that code. – Support Ukraine May 20 '21 at 10:04
  • @M.Kotzjan as shown in the table, the results were achieved with 100 000 - 700 000 random numbers. It's not possible for it to be "worst case result" every time with every counting – Jedrzej May 20 '21 at 10:11
  • 1
    Actually it is possible, if all the numbers are the same, then it results close to the worst case. As I said, with that number of items, try a range of [-10000,10000] and see if the result improves. – sagi May 20 '21 at 10:12
  • @sagi holy hell, that solved it instantly! Thanks! So obvious in hindsight! Solved! – Jedrzej May 20 '21 at 10:14
  • No problem mate – sagi May 20 '21 at 10:17
  • @Jedrzej - To fit SO's Q/A format better, could you write your own answer below (in the answer section) if you've solved it? – General Grievance May 20 '21 at 12:05

0 Answers0