fork(12) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int compare ( const void *pa, const void *pb )
  6. {
  7. const int *a = pa;
  8. const int *b = pb;
  9. if(a[0] == b[0])
  10. return a[1] - b[1];
  11. else
  12. return a[0] - b[0];
  13. }
  14.  
  15. int main(void)
  16. {
  17. srand((unsigned)time(NULL));
  18. int n = 5;
  19. int array[n][2];
  20. int i;
  21.  
  22. for (i = 0; i < n; i++)
  23. {
  24. array[i][0] = rand()%6;
  25. array[i][1] = i;
  26. }
  27. for(i = 0; i < n; ++i)
  28. printf("%2d, %2d\n", array[i][0], array[i][1]);
  29.  
  30. printf("\n");
  31.  
  32. qsort(array, n, sizeof array[0], compare);
  33.  
  34. for(i = 0;i < n;++i)
  35. printf("%2d, %2d\n", array[i][0], array[i][1]);
  36. }
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
 5,  0
 1,  1
 5,  2
 4,  3
 3,  4

 1,  1
 3,  4
 4,  3
 5,  0
 5,  2