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:
- Create a list of random numbers of length 10 Million
- 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)
- Make a list 1 Million Longer
- 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;
}
}
}