Use your rule as a comparison function for qsort (as long as B is greater than A):
#include <stdio.h>
This gives:
$ ./a.out Sorted A A[0] : 4 B[A[0]] : 2 A[1] : 1 B[A[1]] : 3 A[2] : 0 B[A[2]] : 5 A[3] : 7 B[A[3]] : 6 A[4] : 5 B[A[4]] : 7
Qsort_r is also available on many platforms (on linux, you will need #define _GNU_SOURCE before you enable <stdlib.h> to use it. Using this, you will change the comparison function, for example,
int my_cmp(const void *a_, const void *b_,void *arg_) { const int *a = a_, *b = b_, *arg = arg_; if(arg[*a] == arg[*b]) return 0; else if (arg[*a] < arg[*b]) return -1; else return 1; }
And call qsort_r like
qsort_r(A,sizeof A/sizeof A[0] ,sizeof A[0],my_cmp,B);
source share