Originally Posted by

**brewbuck** Then you must have a bug in your sorting algorithm. Look more carefully at my comparison function.

For two elements with unequal z, the one with lower z will come first.

For two elements with EQUAL z, the one with lower t will come first.

Obviously, for a given z0, all elements where z <= z0 will come before elements where z > z0. We can then consider the groups z <= z0 and z > z0 independently. Within these groups, the same argument can be applied, until we have divided all the elements into groups with equal z.

Then, within these groups, it is obvious that the ordering will be by t.

This is nothing more than *lexical ordering*. An "alphabetic sort", where the first letter is the z value, and the second letter is the t value.

I know you comparison function is correct it just isn't working with the code... I'm really laughing at this point as I don't see a conceivable reason why it isn't work. The two bin sort code tidbits (bin 1 and bin 2) are nearly identical but the second bin will not sort time for some reason.

--------------------------

Here's my code at this point:

Code:

#include <stdlib.h>
#include <stdio.h>
struct record
{
double *x;
double *y;
double *z;
double *t;
};
int compare( double z1, double time1, double z2, double time2 )
{
if( z1 < z2 ) return -1;
if( z1 > z2 ) return 1;
if( time1 < time2 ) return -1;
if( time1 > time2 ) return 1;
return 0;
}
int main(int argc, char *argv[])
{
FILE *ifp, *ofp;
ifp = fopen(argv[1], "r"); // Input data file
ofp = fopen(argv[2], "w"); // Output data file
int c; // counter
int a, b; // to store the number in the bins
/* Used for sorting the data */
int i, j;
double temp;
double temps;
double temp3;
/* z sorting */
double s1;
double s2;
double s3;
/* 1st bin*/
double ss1;
double ss2;
double ss3;
/* 2nd bin*/
double sss1;
double sss2;
double sss3;
/* Structure */
struct record tmp;
/* file formats to be read in */
double *x;
double *y;
double *z;
double *t;
const int max_el = 40; // Max array size
int al = 0; // Actual array length
int ap = 0; // array pointer
/* Memory Allocation */
tmp.x = malloc(max_el * sizeof(double));
tmp.y = malloc(max_el * sizeof(double));
tmp.z = malloc(max_el * sizeof(double));
tmp.t = malloc(max_el * sizeof(double));
while(!feof(ifp)) {
al = al + 1;
fscanf(ifp, "%lf,%lf,%lf,%lf\n",
&tmp.x[ap], &tmp.y[ap], &tmp.z[ap],
&tmp.t[ap]);
ap = ap + 1;
}
fclose(ifp);
a = 0; /* Initialize the counters */
b = 0;
/* z bin counting */
for (c = 0; c < al; c++) {
if ((tmp.z[c] >= 0.0)&&(tmp.z[c] <= 100))
a += 1;
else if (tmp.z[c] > 100)
b += 1;
}
/***********************************************/
/* Bubble Sort to sort the data by z */
for (i = (al - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (tmp.z[j-1] > tmp.z[j])
{
temp = tmp.z[j-1];
tmp.z[j-1] = tmp.z[j];
tmp.z[j] = temp;
s1 = tmp.x[j-1];
tmp.x[j-1] = tmp.x[j];
tmp.x[j] = s1;
s2 = tmp.y[j-1];
tmp.y[j-1] = tmp.y[j];
tmp.y[j] = s2;
s3 = tmp.t[j-1];
tmp.t[j-1] = tmp.t[j];
tmp.t[j] = s3;
}
}
}
/***************************************/
**
/* first bin */
for (i = (a - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (compare(tmp.z[j-1], tmp.t[j-1], tmp.z[j], tmp.t[j]) == 1)
{
temp3 = tmp.t[j-1];
tmp.t[j-1] = tmp.t[j];
tmp.t[j] = temp3;
ss1 = tmp.x[j-1];
tmp.x[j-1] = tmp.x[j];
tmp.x[j] = ss1;
ss2 = tmp.y[j-1];
tmp.y[j-1] = tmp.y[j];
tmp.y[j] = ss2;
ss3 = tmp.z[j-1];
tmp.z[j-1] = tmp.z[j];
tmp.z[j] = ss3;
}
}
}
/* second bin */
for (i = (b - 1); i >= a; i--)
{
for (j = (a + 1); j <= i; j++)
{
if (compare(tmp.z[j-1], tmp.t[j-1], tmp.z[j], tmp.t[j]) == 1)
{
temps = tmp.t[j-1];
tmp.t[j-1] = tmp.t[j];
tmp.t[j] = temps;
sss1 = tmp.x[j-1];
tmp.x[j-1] = tmp.x[j];
tmp.x[j] = sss1;
sss2 = tmp.y[j-1];
tmp.y[j-1] = tmp.y[j];
tmp.y[j] = sss2;
sss3 = tmp.z[j-1];
tmp.z[j-1] = tmp.z[j];
tmp.z[j] = sss3;
}
}
}
printf("Sorting complete \n");
fprintf(ofp,"Data Sorted\n");
fprintf(ofp,"The amount in each z bin:\n");
fprintf(ofp,"%d, %d\n", a, b);
fprintf(ofp,"time, z, y, x \n");
for (i = 0; i < a; ++i)
fprintf(ofp,"%lf, %lf, %lf, %lf \n", tmp.t[i], tmp.z[i],tmp.y[i], tmp.x[i]);
fprintf(ofp,"***************\n");
for (i = a; i < al; ++i)
fprintf(ofp,"%lf, %lf, %lf, %lf \n", tmp.t[i], tmp.z[i],tmp.y[i], tmp.x[i]);
**
free(tmp.x);
free(tmp.y);
free(tmp.z);
free(tmp.t);
fclose(ofp);
return 0;
}

produces output:

Code:

time, z, y, x
1.000000, 1.000000, 2.000000, 15.000000
5.000000, 1.000000, 2.000000, 5.000000
6.000000, 11.000000, 22.000000, 5.000000
10.000000, 11.000000, 52.000000, 5.000000
3.000000, 13.000000, 2.000000, 45.000000
7.000000, 15.000000, 42.000000, 5.000000
8.000000, 51.000000, 2.000000, 5.000000
***************
**4.000000**, 111.000000, 32.000000, 5.000000
**2.000000**, 122.000000, 2.000000, 25.000000
9.000000, 221.000000, 2.000000, 5.000000

still gives me the 2nd bin not sorted correctly

Pretty confident right now the problem lies in the condition statements for the 2nd bins sorting:

IE

Code:

/* second bin */
for (i = (b - 1); i >= a; i--)
{
for (j = (a + 1); j <= i; j++)
{

As in this case: A count was 7 and B count was 3... the first condition wont decrease if the 2nd elevation bin (which is the b count) has less number of records then the first bin (the a count)

Looking to see if there's way to say, directly for the array records, that after the 7 array record resort again for t?