fork download
  1. #include <stdio.h>
  2.  
  3. typedef struct {
  4. int id;
  5. int weight;
  6. int height;
  7. } Body;
  8.  
  9. void swap(Body *a, Body *b) {
  10. Body tmp;
  11. tmp = *a;
  12. *a = *b;
  13. *b = tmp;
  14. }
  15.  
  16. int main(void) {
  17. int i, j;
  18.  
  19. Body a[] = {
  20. {1, 65, 169},
  21. {2, 73, 170},
  22. {3, 59, 161},
  23. {4, 79, 175},
  24. {5, 55, 168}
  25. };
  26.  
  27. int n = sizeof(a) / sizeof(a[0]);
  28.  
  29. for (i = 0; i < n - 1; i++) {
  30. for (j = 0; j < n - 1 - i; j++) {
  31. if (a[j].height < a[j + 1].height) {
  32. swap(&a[j], &a[j + 1]);
  33. }
  34. }
  35. }
  36.  
  37. for (i = 0; i < n; i++) {
  38. printf("id=%d weight=%d height=%d\n",
  39. a[i].id, a[i].weight, a[i].height);
  40. }
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5328KB
stdin
Standard input is empty
stdout
id=4 weight=79 height=175
id=2 weight=73 height=170
id=1 weight=65 height=169
id=5 weight=55 height=168
id=3 weight=59 height=161