I have two integer arrays, and I am trying to sort the first array based on the other array.
eg. a = {1,2,3,6,0,0,0};
and b = {1,2,2,0,0,0,0};
the values sorted in b is the real value for each integer in a
the expected result I am expecting after sorting is:
a = {2,3,1,6,0,0,0};
b = {2,2,1,0,0,0,0};
this is the code I used
int j,k,temp1,temp2;
for (j=0; j<N; j++){
for (k=j+1; k<N; k++){
if (b[j] < b[k]){
temp1 = b[j];
b[j] = b[k];
b[k] = temp1;
temp2 = a[j];
a[j] = a[k];
a[k] = temp2;
}
}
}
it gives me output: a = {2,3,1,0,0,0,6};
and b = {2,2,1,0,0,0,0};
I can't tell where the mistakes are, any help and suggestion is appreciated.
puts("swap")inside the if condition to check when 2 values are swapped and it happens 2 times in your case. You might probably have compiled the wrong source code