fork(7) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int array1[] = {1, 7, 3, 9, 5};
  5. double array2[] = {1.1, 7.7, 3.3, 9.9, 5.5};
  6.  
  7. int compare (const void * a, const void * b) {
  8. double diff = array2[*(int*)a] - array2[*(int*)b];
  9. return (0 < diff) - (diff < 0);
  10. }
  11.  
  12. int main(void) {
  13. int perm[5], i;
  14. int res[5];
  15. for (i = 0 ; i != 5 ; i++) {
  16. perm[i] = i;
  17. }
  18. qsort (perm, 5, sizeof(int), compare);
  19. for (i = 0 ; i != 5 ; i++) {
  20. res[i] = array1[perm[i]];
  21. }
  22. for (i = 0 ; i != 5 ; i++) {
  23. printf("%d\n", res[i]);
  24. }
  25. return 0;
  26. }
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
1
3
5
7
9