Hey hey,
So I've encountered a weird problem that I am not sure how to work around. I'm trying to calculate distances between particles in a system, store them in an array, sort it, and extract certain information from it. However, my distance array contains no zeros before the sort, yet after the first element is always 0. I don't think it's the sorting algorithm as it works fine on other arrays, but I have included it anyway.
Code:
void quickSort(double *arr, int elements) {
#define MAX_LEVELS 300
int beg[MAX_LEVELS], end[MAX_LEVELS], i=0, L, R;
float swap, piv;
beg[0]=0; end[0]=elements;
while (i>=0) {
L=beg[i]; R=end[i]-1;
if (L<R) {
piv=arr[L];
while (L<R) {
while (arr[R]>=piv && L<R) R--; if (L<R) arr[L++]=arr[R];
while (arr[L]<=piv && L<R) L++; if (L<R) arr[R--]=arr[L]; }
arr[L]=piv; beg[i+1]=L+1; end[i+1]=end[i]; end[i++]=L;
if (end[i]-beg[i]>end[i-1]-beg[i-1]) {
swap=beg[i]; beg[i]=beg[i-1]; beg[i-1]=swap;
swap=end[i]; end[i]=end[i-1]; end[i-1]=swap; }}
else {
i--; }}}
/* End QuickSort() */
Here is where I think the problem is, and I feel like it has something to do with the indexing of the array but I can't seem to fix it.
Code:
for(k = 0; k < 1; k++){
SIZE = NUM_A-k-1-1;
rA = (double *)malloc(SIZE * sizeof(double));
printf("Size = %d\n", SIZE);
for (i=k+1; i < NUM_A; i++ ){
//if (k==i){continue;}
rx = (x[k] - x[i])- Lx*rintl((x[k] - x[i])/Lx);
ry = (y[k] - y[i])- Ly*rintl((y[k] - y[i])/Ly);
rz = (z[k] - z[i])- Lz*rintl((z[k] - z[i])/Lz);
rA[i] = sqrt( rx*rx + ry*ry + rz*rz);
printf("r[%d][%d] = %lf\n", k, i, rA[i]);
}
quickSort(rA, SIZE);
printf("Sorted\n");
for(i = 0; i < SIZE; i++){ printf("r[%d][%d] = %lf\n", k, i, rA[i]); }