#include #include #include #define UNLIMIT #define MAXARRAY 60000 /* this number, if too large, will cause a seg. fault!! */ struct my3DVertexStruct { int x, y, z; double distance; }; int compare_my3DVertexStruct(const void *elem1, const void *elem2) { /* D = [(x1 - x2)^2 + (y1 - y2)^2 + (z1 - z2)^2]^(1/2) */ /* sort based on distances from the origin... */ double distance1, distance2; distance1 = (*((struct my3DVertexStruct *)elem1)).distance; distance2 = (*((struct my3DVertexStruct *)elem2)).distance; return (distance1 > distance2) ? 1 : ((distance1 == distance2) ? 0 : -1); } int main_qsort_large(int argc, char *argv[]) { struct my3DVertexStruct array[MAXARRAY]; FILE *fp; int i,count=0; int x, y, z; if (argc<2) { fprintf(stderr,"Usage: qsort_large \n"); exit(-1); } else { fp = fopen(argv[1],"r"); while((fscanf(fp, "%d", &x) == 1) && (fscanf(fp, "%d", &y) == 1) && (fscanf(fp, "%d", &z) == 1) && (count < MAXARRAY)) { array[count].x = x; array[count].y = y; array[count].z = z; array[count].distance = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)); count++; } } printf("\nSorting %d vectors based on distance from the origin.\n\n",count); qsort(array,count,sizeof(struct my3DVertexStruct),compare_my3DVertexStruct); for(i=0;i