#include
#include
int comp(const void *, const void *);
/* for sorting a float array */
int comp(const void *p, const void *q)
{
return ((*(float *)p - *(float *)q) > 0.0 ? 1 : -1);
}
int main(int argc, char *argv[])
{
int i,j;
float *array_1,*array_2;
srand ( time(NULL) );
array_1 = (float*)calloc(sizeof(float),10);
printf("qsort start \n");
for(i=0;i<10;i++)
{
*(array_1 + i ) = (rand()%200)/7.0;
}
for(i=0;i<10;i++)
{
printf("array_1 %d = %f \n",i,*(array_1 + i));
}
printf("-------- \n");
qsort(array_1, 10, sizeof(float), comp);
for(i=0;i<10;i++)
{
printf("array_1 %d = %f \n",i,*(array_1 + i));
}
printf("\n");
return 0;
}
沒有留言:
張貼留言