So I try to sort an array with qsort and then save the new order, so this means if I have a list like:
4 3 7 2
[0] [1] [2] [3],
after sorting it becomes:
2 3 4 7
[3] [1] [0] [2] <== this is what I want to have!
Code:
void qsort_my(int *a, int l, int r, int *array_order)
{
int j;
if( l < r )
{
j = split( a, l, r, array_order);
qsort_my( a, l, j-1, array_order);
qsort_my( a, j+1, r, array_order);
}
}
int split(int *a, int l, int r, int *array_order) {
int pivot, i, j, t;
pivot = a[l];
i = l; j = r+1;
int switch_pos_temp;
while( 1)
{
while( a[i] <= pivot && i <= r ) do ++i;
while( a[j] > pivot ) do --j;
if( i >= j ) break;
t = a[i]; switch_pos_temp = i;
a[i] = a[j]; array_order[i] = j;
a[j] = t; array_order[j] = switch_pos_temp;
}
t = a[l]; switch_pos_temp = l;
a[l] = a[j]; array_order[l] = j;
a[j] = t; array_order[j] = switch_pos_temp;
return j;
}
But my problem is that the list gets sorted, but the indexes do strange stuff. When I run with this list:
4 8 14 1 2 1 22 12 2 14
Pos: 0 1 2 3 4 5 6 7 8 9
I get:
1 1 2 2 4 8 12 14 14 22
Pos: 1 0 1 0 0 5 7 6 5 6
And with some printfs I noticed, the first two calls of split it is fine but then strange things start to happen, somebody have any suggestion what I do wrong?