fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int** ordena(int **v, int tam) {
  5. int i, j,aux;
  6. int swap[3];
  7. for (i = 0; i < tam-1; i++) {
  8. aux = i;
  9. for (j = (i+1); j < tam; j++) {
  10. if(v[j][1] < v[aux][1]) {
  11. aux = j;
  12. }
  13. }
  14. if (i != aux) {
  15. swap[0] = v[i][0];
  16. swap[1] = v[i][1];
  17. swap[2] = v[i][2];
  18. v[i][0] = v[aux][0];
  19. v[i][1] = v[aux][1];
  20. v[i][2] = v[aux][2];
  21. v[aux][0] = swap[0];
  22. v[aux][1] = swap[1];
  23. v[aux][2] = swap[2];
  24. }
  25. }
  26. return v;
  27. }
  28.  
  29. int main() {
  30.  
  31. int **array2d = malloc(sizeof(*array2d) * 4);
  32. array2d[0] = malloc(sizeof(*array2d[0]) * 3);
  33. array2d[1] = malloc(sizeof(*array2d[0]) * 3);
  34. array2d[2] = malloc(sizeof(*array2d[0]) * 3);
  35. array2d[3] = malloc(sizeof(*array2d[0]) * 3);
  36.  
  37. array2d[0][0] = 2;
  38. array2d[0][1] = 3;
  39. array2d[0][2] = 4;
  40.  
  41. array2d[1][0] = 3;
  42. array2d[1][1] = 7;
  43. array2d[1][2] = 9;
  44.  
  45. array2d[2][0] = 1;
  46. array2d[2][1] = 1;
  47. array2d[2][2] = 1;
  48.  
  49. array2d[3][0] = 5;
  50. array2d[3][1] = 2;
  51. array2d[3][2] = 4;
  52.  
  53. ordena(array2d, 4);
  54.  
  55. int i,j;
  56. for (i = 0; i < 4; ++i) {
  57. for (j = 0; j < 3; ++j) {
  58. printf("%d ", array2d[i][j]);
  59. }
  60.  
  61. printf("\n");
  62. }
  63.  
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0s 4536KB
stdin
Standard input is empty
stdout
1 1 1 
5 2 4 
2 3 4 
3 7 9