so i have this counting sort code and i need to test how fast does it work for certain number of numbers. 10,1000,5000,10000 etc. now i have a problem,i found some code for execution time in C and it shows no errors,but whenever i start it i always have same result. 0.000000 seconds or milliseconds even nanoseconds. so this is my code:
void CountingSort(int array[], int k, int n,int min)
{
int i, j;
int B[50000], C[100000];
for (i = 0; i <= k+min; i++){
C[i] = 0;
}
for (j = 0; j < n; j++){
C[array[j]+min]++;
}
for (i = 1; i <= k+min; i++){
C[i] += C[i-1];
}
for (j = 0; j < n; j++) {
B[--C[array[j]+min]] = array[j];
}
printf("Counting sort: \n");
for (i = 0; i < n; i++) {
printf("%d ", B[i]);
}
printf("\n");
}
void minimum(int array[],int *min,int n){
int i;
for (i = 0; i < n; i++)
{
if (array[i] < *min) {
*min= array[i];
}
}
}
void max(int array[],int *k,int n){
int i;
for (i = 0; i < n; i++)
{
if (array[i] > *k) {
*k = array[i];
}
}
}
int main(int brArg,char *arg[])
{
int array[1000000];
int i=0,j,k=0,n,x,z,min=0,prsk=0;
printf("Ten random numbers in [1,100]\n");
for (i = 0; i < 5000; i++) {
n = rand() % 200 + 1;
printf("I[%d]=%d\n",i, n);
array[i]=n;
printf("arr %d \n",array[i]);
}
n=i;
max(array,&k,n);
minimum(array,&min,n);
min=abs(min);
clock_t t;
double time_spent;
t = clock();
CountingSort(array,k,n,min);
t = clock() - t;
double time_taken = ((double)t)/(CLOCKS_PER_SEC/1000000000.0); // in seconds
printf("counting took %f miliseconds to execute \n", time_taken);
return 0;
}
now you dont have to look at the code how it works or anything it works fine i only need help for that time testing at the end,what i did wrong so time test doesent work?
Aucun commentaire:
Enregistrer un commentaire