Luke Collins
Luke Collins

Reputation: 1463

Why can't my program work for large arrays?

I posted this question on the site earlier, whose solution I managed to get to (more or less). In a nutshell, I need to test the insertion and quicksort algorithms for arrays of various sizes, and see how their runtime varies with respect to array size.

The only issue is that my program seems to freeze when it tries to calculate the runtime of the quicksort algorithm for arrays with 100 elements and larger. I've tried debugging the code and I can't seem to understand why this is the case. When I run it, this is the output I get:

enter image description here

Why does it stop there? and why is the runtime zero? Can anyone help me with this? In my original question, some commenters suggested I use malloc but I'm not sure how to go about it.

My code is listed below, I'd appreciate any suggestions.

/*
 * Task 1, question h
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//Random Array Length
#define MAX 1000

void perf_routine(int);
void naive_sort(int[],int);
void smarter_sort(int[],int,int);
void swap(int[],int,int);
int choose_piv(int[],int,int);

int main(){
    perf_routine(10);
    perf_routine(100);
    perf_routine(1000);
    perf_routine(5000);
    perf_routine(10000);
   return 0;
}

void perf_routine(int L){
    int i, a[L], b[L];
        clock_t tic, toc;

        printf("Arrays of Length %d:\n", L);

        //Generate an array of random numbers
        for(i=0; i<L; i++)
            a[i]= rand() % (MAX+1);

        //Define b identical to a for fair comparison
        for(i=0; i<L; i++)
            b[i]=a[i];

        //Insertion Sort (1e)
        tic = clock();
        naive_sort(a, L);
        toc = clock();
        printf("Insertion Sort Runtime: %f seconds\n", (double)(toc-tic)/CLOCKS_PER_SEC);

        //Quicksort (1f)
        tic = clock();
        smarter_sort(b,0,L-1);
        toc = clock();
       printf("Quicksort Runtime: %f seconds\n", (double)(toc-tic)/CLOCKS_PER_SEC);
}

void naive_sort(int a[], int L){
    int i, j, t;
    for(i=1; i < L; i++){
        t=a[i];
        j=i-1;
        while((j >= 0) && (t < a[j])){
            a[j+1] = a[j];
            j--;
        }
        a[j+1]=t;
    }
}

void smarter_sort(int a[], int l, int r){
    if(r > l){
        int piv = choose_piv(a, l, r);
        smarter_sort(a, l, piv-1);
        smarter_sort(a, piv+1, r);
    }
}

void swap(int a[], int i, int j){
    int t=a[i];
    a[i]=a[j];
    a[j]=t;
}

int choose_piv(int a[], int l, int r){
    int pL = l, pR = r;
    int piv = l;
    while (pL < pR){
        while(a[pL] < a[piv])
            pL++;
        while(a[pR] > a[piv])
            pR--;
        if(pL < pR)
            swap(a, pL, pR);
    }
    swap(a, piv, pR);
    return pR;
}

Upvotes: 2

Views: 68

Answers (1)

Tom Karzes
Tom Karzes

Reputation: 24052

choose_piv can go into an infinite loop if there are duplicate values in the array. If a[pL], a[pR], and a[piv] are the same, then the inner while loops both exit immediately, the swap has no effect (since both values are the same), and the outer while loop will loop forever. Try it with a small array where all element are the same (e.g., all zero).

Upvotes: 1

Related Questions