2

you guys have helped me so much with this code. Let me preface by saying I do not know C very well and am trying really hard to do this.

This is what the program should do:

  1. Create a list of random numbers of length 10 Million
  2. Sort the list of random numbers using shell sort function (still doesn't work properly...i think its how I am passing the pointer to the function)
  3. Make a list 1 Million Longer
  4. repeat for up to 100 million while recording time (the time shows up as 0.0000000 for some reason)

I'm just trying to test this shell sort program vs quick sort built into the standard library.

I've tried with and without pointers. The commented out section should work when it's done. It just messes things up more lol

Please help me out, you guys have been so great so far...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


void shellSort(int *A, int n);
void checkSort(int *A, int n);

int main(){

    /*Initialize Random Array*/
    int unsorted_list[10000000];
    int *ptr = &unsorted_list[0];
    int random_number;
    int i;

    srand ( time(NULL) );
    for(i=0; i<10000000; i++){

        random_number = rand();
        unsorted_list[i] = random_number % 10000000;
    }

    //Do C Shell Sort
    double shell_results[10][2];

    double clock_diff;
    int j=10000000;
    clock_t t0, t1;
    int k;


    for(i=0;i<10;i++){



        /*Sort the list using shellSort and take the time difference*/
        t0 = clock();
        shellSort(ptr, j);
        t1= clock();

        /*Take difference in time*/
        clock_diff = (t1 - t0)/CLOCKS_PER_SEC;

        /*Add time and list length to the results array*/
        shell_results[i][0] = (double)j;
        shell_results[i][1] = clock_diff;


        /*Check to make sure the array has been sorted*/
        checkSort(ptr, j);

        /*Re-initialize a longer array*/
        //j+=1000000;
        //for(k=0; k<j; k++){
        //    random_number = rand();
        //    unsorted_list[k] = random_number % 1000000;
        //}

        printf("%d",(int)shell_results[i][0]);
        printf(" ");
        printf("%f",shell_results[i][1]);
        printf("\n");

    }





 return 0;
 }

 void shellSort(int *A, int n){



     int gap , i , j , temp;

     for (gap = n/2; gap>0; gap /=2)
         for (i=gap; i<n; i++)
             for(j = i-gap; j>=0 && A[j] > A[j+gap]; j-=gap){
                 temp = A[j];
                 A[j] = A[j + gap];
                 A[j + gap] = temp;
     }
 }



void checkSort(int *A, int n){

    int i;

    for(i=0;i<n;i++){

        if(A[i]>A[i+1]){

            printf("Error in sorting \n");
            break;
        }
    }
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
Chris Feher
  • 111
  • 1
  • 9
  • [Explanation of why allocating a large array results in segmentation fault in C](https://stackoverflow.com/q/30864358/995714), [Segmentation fault on large array sizes](https://stackoverflow.com/q/1847789/995714), [Why do I get a segfault in C from declaring a large array on the stack?](https://stackoverflow.com/q/3144135/995714), [why does this large array declaration produce a segmentation fault?](https://stackoverflow.com/q/3049934/995714) – phuclv Sep 02 '19 at 01:27
  • Possible duplicate of [C programming, why does this large array declaration produce a segmentation fault?](https://stackoverflow.com/questions/3049934/c-programming-why-does-this-large-array-declaration-produce-a-segmentation-faul) – phuclv Sep 02 '19 at 01:27

2 Answers2

5

You probably don't have 10 megabytes of stack space. Make that array global, declare it with static, or allocate it dynamically using malloc(). If you choose the latter, don't forget to free() it.

Later, when you need to use the 100,000,000 element array, make sure to use a new allocation for it!

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

Well there is no way you are going to have that amount of space available on the stack. Allocate it off the heap using malloc(). Remember to free() it afterwards.

Iowa15
  • 3,027
  • 6
  • 28
  • 35