Sorting problem with structures and arrays, could I get some guidance?
Task: Sorting a array data set that I made a structure for.
Arrays are in x, y, z, time....
First I count the number of records of z that fall into two bins ( 0 < z < 100 and z > 100)
I then sort the data set for z in ascending order no problem. I use a structure and have swaps so that other data records (x, y, and time) stay with the z record.
Now the problem arises. I have sorted the data by z and now want to sort it further by time for just those two sections. What I mean is - if the first bin say collects 30 and second collects 10. Then I want it to sort the first 30 items in the array records by time and then sort the last 10 by time - so to keep the distinction of the two bins by z in tact (if that makes any sense). Tried multiple things and just can't get it to work.
Seems the first sort works fine - just using a bubble sort (don't hate - its a small data set). But can't get the rest to work.
Full code:
Code:
#include <stdlib.h>
#include <stdio.h>
struct record
{
double *x;
double *y;
double *z;
double *t;
};
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;
int iii, jjj;
double temps;
int iiii, jjjj;
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 (iii = (a - 1); iii >= a; iii--)
{
for (jjj = 1; jjj <= iii; jjj++)
{
if (tmp.t[jjj-1] > tmp.t[jjj])
{
temp3 = tmp.t[jjj-1];
tmp.t[jjj-1] = tmp.t[jjj];
tmp.t[jjj] = temp3;
ss1 = tmp.x[jjj-1];
tmp.x[jjj-1] = tmp.x[jjj];
tmp.x[jjj] = ss1;
ss2 = tmp.y[jjj-1];
tmp.y[jjj-1] = tmp.y[jjj];
tmp.y[jjj] = ss2;
ss3 = tmp.z[jjj-1];
tmp.z[jjj-1] = tmp.z[jjj];
tmp.z[jjj] = ss3;
}
}
}
/* second bin */
for (iiii = (b - 1); iiii >= b; iiii--)
{
for (jjjj = a; jjjj <= iiii; jjjj++)
{
if (tmp.t[jjjj-1] > tmp.t[jjjj])
{
temps = tmp.t[jjjj-1];
tmp.t[jjjj-1] = tmp.t[jjjj];
tmp.t[jjj] = temps;
sss1 = tmp.x[jjj-1];
tmp.x[jjjj-1] = tmp.x[jjjj];
tmp.x[jjjj] = sss1;
sss2 = tmp.y[jjjj-1];
tmp.y[jjjj-1] = tmp.y[jjjj];
tmp.y[jjjj] = sss2;
sss3 = tmp.z[jjj-1];
tmp.z[jjjj-1] = tmp.z[jjjj];
tmp.z[jjjj] = sss3;
}
}
}
printf("Sorting complete \n");
fprintf(ofp,"Data Sorted");
fprintf(ofp,"The amount in each z bin:\n");
fprintf(ofp,"%d, %d\n", a, b);
for (iii = 0; iii < a; ++iii)
fprintf(ofp,"%lf, %lf, %lf, %lf \n", tmp.t[iii], tmp.x[iii],tmp.y[iii], tmp.z[iii]);
for (i = a; i < b-1; ++i)
fprintf(ofp,"%lf, %lf, %lf, %lf \n", tmp.t[iiii], tmp.x[iiii],tmp.y[iiii], tmp.z[iiii]);
free(tmp.x);
free(tmp.y);
free(tmp.z);
free(tmp.t);
fclose(ofp);
return 0;
}
Troublesome part of the code
Code:
/* first bin */
for (iii = (a - 1); iii >= a; iii--)
{
for (jjj = 1; jjj <= iii; jjj++)
{
if (tmp.t[jjj-1] > tmp.t[jjj])
{
temp3 = tmp.t[jjj-1];
tmp.t[jjj-1] = tmp.t[jjj];
tmp.t[jjj] = temp3;
ss1 = tmp.x[jjj-1];
tmp.x[jjj-1] = tmp.x[jjj];
tmp.x[jjj] = ss1;
ss2 = tmp.y[jjj-1];
tmp.y[jjj-1] = tmp.y[jjj];
tmp.y[jjj] = ss2;
ss3 = tmp.z[jjj-1];
tmp.z[jjj-1] = tmp.z[jjj];
tmp.z[jjj] = ss3;
}
}
}
/* second bin */
for (iiii = (b - 1); iiii >= b; iiii--)
{
for (jjjj = a; jjjj <= iiii; jjjj++)
{
if (tmp.t[jjjj-1] > tmp.t[jjjj])
{
temps = tmp.t[jjjj-1];
tmp.t[jjjj-1] = tmp.t[jjjj];
tmp.t[jjj] = temps;
sss1 = tmp.x[jjj-1];
tmp.x[jjjj-1] = tmp.x[jjjj];
tmp.x[jjjj] = sss1;
sss2 = tmp.y[jjjj-1];
tmp.y[jjjj-1] = tmp.y[jjjj];
tmp.y[jjjj] = sss2;
sss3 = tmp.z[jjj-1];
tmp.z[jjjj-1] = tmp.z[jjjj];
tmp.z[jjjj] = sss3;
}
}
}
printf("Sorting complete \n");
fprintf(ofp,"Data Sorted");
fprintf(ofp,"The amount in each z bin:\n");
fprintf(ofp,"%d, %d\n", a, b);
for (iii = 0; iii < a; ++iii)
fprintf(ofp,"%lf, %lf, %lf, %lf \n", tmp.t[iii], tmp.x[iii],tmp.y[iii], tmp.z[iii]);
for (i = a; i < b-1; ++i)
fprintf(ofp,"%lf, %lf, %lf, %lf \n", tmp.t[iiii], tmp.x[iiii],tmp.y[iiii], tmp.z[iiii]);