#define u32 uint32_t
struct edge {
u32 v1;
u32 v2;
};
struct edge *array = malloc((sizeof(struct edge))*Narray);
int struct_cmp_by_v1(const void *i, const void *j) {
struct edge *a = (struct edge *)i;
struct edge *b = (struct edge *)j;
u32 x = a->v1;
u32 y = b->v1;
if(x < y)
return -1;
else if(x == y)
return 0;
else
return 1;
}
qsort(array, (size_t)(sizeof(array)/sizeof(struct edge)), sizeof(struct edge), struct_cmp_by_v1);
I want to arrange an array of struct and qsort should only compare one of the fields of the struct so that the structures are ordered keeping the invariant that the v1 has its corresponding partner v2 even after being ordered by the v1, the case is that compiles with the flag -O3 well although it orders, the output is disordered in a certain way as many numbers of u32 to order there are many that are repeated and some many that very little anyway there is no order.
qsort orders but leaves it messy and at the end of the execution, I ask a function to check the order and exit:
amount of elements in the array:25056012
begins to order
finished ordering
1818004993
1343749791
1343749791 < 1818004993
index: 1 < 0
I do not understand what I can be ignoring, if anyone can give me a help I would appreciate it very much