fork download
  1. #include <stdio.h>
  2. #include <memory.h>
  3.  
  4. const int SORT_COLUMN_NUMBER = 0;
  5. const int SORT_COLUMN_HEIGHT = 1;
  6. const int SORT_COLUMN_WEIGHT = 3;
  7.  
  8. const int SORT_ALGORITHM_SELECTIONSORT = 0;
  9. const int SORT_ALGORITHM_BUBBLESORT = 1;
  10. const int SORT_ALGORITHM_QUICKSORT = 2;
  11.  
  12. const int SORT_ORDER_ASCENDING = 0;
  13. const int SORT_ORDER_DESCENDING = 1;
  14.  
  15. void swap(int x[], int y[]) {
  16. int t[5];
  17. memcpy(t, x, sizeof(t));
  18. memcpy(x, y, sizeof(t));
  19. memcpy(y, t, sizeof(t));
  20. }
  21.  
  22. void selectionsort(int data[][5], int count, int column, int order) {
  23. int i;
  24. int j;
  25. int k;
  26.  
  27. for (i = 0; i < count - 1; i++) {
  28. j = i;
  29. for (k = i + 1; k < count; k++) {
  30. if (order == SORT_ORDER_ASCENDING) {
  31. if (data[k][column] < data[j][column]) {
  32. j = k;
  33. }
  34. } else {
  35. if (data[j][column] < data[k][column]) {
  36. j = k;
  37. }
  38. }
  39. }
  40. swap(data[i], data[j]);
  41. }
  42. }
  43.  
  44. void bubblesort(int data[][5], int count, int column, int order) {
  45. int i;
  46. int j;
  47.  
  48. for (i = count - 1; 0 < i; i--) {
  49. for (j = 0; j < i; j++) {
  50. if (order == SORT_ORDER_ASCENDING) {
  51. if (data[j + 1][column] < data[j][column]) {
  52. swap(data[j], data[j + 1]);
  53. }
  54. } else {
  55. if (data[j][column] < data[j + 1][column]) {
  56. swap(data[j], data[j + 1]);
  57. }
  58. }
  59. }
  60. }
  61. }
  62.  
  63. void quicksort_(int data[][5], int left, int right, int column, int order) {
  64. int lp;
  65. int rp;
  66. int pivot;
  67.  
  68. if (left >= right) {
  69. return;
  70. }
  71. pivot = data[left][column];
  72. lp = left;
  73. rp = right;
  74. while (lp <= rp) {
  75. if (order == SORT_ORDER_ASCENDING) {
  76. while (data[lp][column] < pivot) {
  77. lp++;
  78. }
  79. } else {
  80. while (pivot < data[lp][column]) {
  81. lp++;
  82. }
  83. }
  84. if (order == SORT_ORDER_ASCENDING) {
  85. while (pivot < data[rp][column]) {
  86. rp--;
  87. }
  88. } else {
  89. while (data[rp][column] < pivot) {
  90. rp--;
  91. }
  92. }
  93. if (lp > rp) {
  94. break;
  95. }
  96. swap(data[lp], data[rp]);
  97. lp++;
  98. rp--;
  99. }
  100. quicksort_(data, left, rp, column, order);
  101. quicksort_(data, lp, right, column, order);
  102. }
  103.  
  104. void quicksort(int data[][5], int count, int column, int order) {
  105. quicksort_(data, 0, count - 1, column, order);
  106. }
  107.  
  108. void sort(int data[][5], int count, int column, int algorithm, int order) {
  109. if (0) {
  110. } else if (algorithm == SORT_ALGORITHM_SELECTIONSORT) {
  111. selectionsort(data, count, column, order);
  112. } else if (algorithm == SORT_ALGORITHM_BUBBLESORT) {
  113. bubblesort(data, count, column, order);
  114. } else if (algorithm == SORT_ALGORITHM_QUICKSORT) {
  115. quicksort(data, count, column, order);
  116. }
  117. }
  118.  
  119. int main(void) {
  120. int data[5][5] = {{0}};
  121. const int number = 0;
  122. const int height = 1;
  123. const int heightDiff = 2;
  124. const int weight = 3;
  125. const int weightDiff = 4;
  126. const int count = 5;
  127.  
  128. int ave_heigh;
  129. int ave_weight;
  130. int sum;
  131. int i;
  132.  
  133. /* input */
  134. for (i = 0; i < 5; i++) {
  135. printf("%d:", i);
  136. scanf("%d,%d,%d", &data[i][number], &data[i][height], &data[i][weight]);
  137. }
  138.  
  139. /* compute average height */
  140. sum = 0;
  141. for (i = 0; i < 5; i++) {
  142. sum += data[i][height];
  143. }
  144. ave_heigh = sum / 5;
  145.  
  146. /* compute difference height */
  147. for (i = 0; i < 5; i++) {
  148. data[i][heightDiff] = data[i][height] - ave_heigh;
  149. }
  150.  
  151. /* compute average weight */
  152. sum = 0;
  153. for (i = 0; i < 5; i++) {
  154. sum += data[i][weight];
  155. }
  156. ave_weight = sum / 5;
  157.  
  158. /* compute difference weight */
  159. for (i = 0; i < 5; i++) {
  160. data[i][weightDiff] = data[i][weight] - ave_weight;
  161. }
  162.  
  163. /* sort */
  164. sort(data, count, SORT_COLUMN_NUMBER, SORT_ALGORITHM_SELECTIONSORT, SORT_ORDER_ASCENDING);
  165. sort(data, count, SORT_COLUMN_HEIGHT, SORT_ALGORITHM_BUBBLESORT, SORT_ORDER_DESCENDING);
  166. sort(data, count, SORT_COLUMN_WEIGHT, SORT_ALGORITHM_QUICKSORT, SORT_ORDER_ASCENDING);
  167.  
  168. /* output */
  169. for (i = 0; i < count; i++) {
  170. printf("%d, %d, %d, %d, %d\n", data[i][0], data[i][1], data[i][2], data[i][3], data[i][4]);
  171. }
  172. printf("average height:%d\n", ave_heigh);
  173. printf("average weight:%d\n", ave_weight);
  174.  
  175. return 0;
  176. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty