I tried to implement an algorithm which sorts the array r of length DIM*n using an array of length n. I don't see where my code is wrong. I don't get the expected result. The result should look like a space filling Morton curve. But as you can see, the result consists of a lot of zeros. I don't know where they come from? Can you please help me to find the error? Here is my executable code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define DIM 2
// Sort list "r" using list "mInt"
void sort(unsigned int *mInt, double *r, int n){
unsigned int i, j, ph0;
double ph1, ph2;
for(i = 1; i <= n-1; i++)
for(j = 1; j <= n-i; j++)
if(mInt[j-1] >= mInt[j])
{
// 1
ph1 = r[DIM*(j-1)+0];
ph2 = r[DIM*(j-1)+1];
ph0 = mInt[j-1];
// 2
mInt[j-1] = mInt[j];
r[DIM*(j-1)+0] = r[DIM*j+0];
r[DIM*(j-1)+1] = r[DIM*j+1];
// 3
mInt[j] = ph0;
r[DIM*j+0] = ph1;
r[DIM*j+1] = ph2;
}
}
// Create morton key
inline unsigned int mortoncode(unsigned int x, unsigned int y){
int answer = 0;
for (unsigned int i = 0; i < (sizeof(unsigned int)* 8)/2; ++i) {
answer |= (((x & ((unsigned int)1 << i)) << 2*i) | ((y & ((unsigned int)1 << i)) << (2*i + 1)));
}
return answer;
}
// Find max / min values
double maxValue(double *r, int n, int d){
double max = r[d];
for(int k=0; k<n; k++){
if(max < r[DIM*k+d]){
max = r[DIM*k+d];
}
}
return max;
}
double minValue(double *r, int n, int d){
double min = r[d];
for(int k=0; k<n; k++){
if(min > r[DIM*k+d]){
min = r[DIM*k+d];
}
}
return min;
}
int main(int argc, char **argv){
FILE *f = fopen("data.dat", "w");
int n = 100;
double r[n*DIM];
// Initialize data
double x1 = 0;
double y1 = 0;
for(int k=0; k<n; k++){
r[DIM*k+0] = x1;
r[DIM*k+1] = y1;
x1 += 0.1;
if(k % 10 == 0){
y1 += 0.1;
x1 = 0;
}
printf("%lf %lf\n", r[DIM*k+0], r[DIM*k+1]);
}
// Get max/min values
double rMin[DIM];
double rMax[DIM];
for(int d=0; d<DIM; d++){
rMin[d] = minValue(r, n, d);
rMax[d] = maxValue(r, n, d);
}
// Convert double data to integers
printf("\n");
unsigned int rInt[n];
for(int k=0; k<n; k++){
for(int d=0; d<DIM; d++){
int idx=DIM*k+d;
double map = floor(((2097151)/(rMax[d]-rMin[d]))*r[idx]-rMin[d]);
rInt[idx] = (int)map;
}
printf("%d %d\n", rInt[DIM*k+0], rInt[DIM*k+1]);
}
// Convert rInt[x_1,y_1,x_2,y_2,...] to Morton key
printf("\n");
unsigned int rMor[n];
for(int k=0; k<n; k++){
int idx = DIM*k;
rMor[k] = mortoncode(rInt[idx+0], rInt[idx+1]);
}
// Sort data
sort(rMor, r, n);
for(int k=0; k<n; k++){
printf("%lf %lf\n", r[DIM*k+0], r[DIM*k+1]);
fprintf(f, "%lf, %lf\n", r[DIM*k+0], r[DIM*k+1]);
}
return 0;
}
unsigned int rInt[n]is not large enough. You probably get error before it reachessortdata.datis an output file, where I save my data. You see the output also in the terminal. See last part of codeprintf("%lf %lf\n", r[DIM*k+0], r[DIM*k+1]);andfprintf(f, "%lf, %lf\n", r[DIM*k+0], r[DIM*k+1]);.r. Something with thesortfunction must be wrong. But I don't find the error.rand01pm()? A special function or a global replace gone bad? I checked @BarmakShemirani'srInt[n]but I don't seemapvalues much greater than 2.0e+06 so they should fit into an unsigned int.rInt[n*DIM]andrMor[n*DIM]or more, otherwise you get overflow. Also this first part of your code is not relevant to the question. You could just fill the arrays with random values and focus on the sort.