#include <stdio.h>
#include <memory.h>
const int SORT_COLUMN_NUMBER = 0;
const int SORT_COLUMN_HEIGHT = 1;
const int SORT_COLUMN_WEIGHT = 3;
const int SORT_ALGORITHM_SELECTIONSORT = 0;
const int SORT_ALGORITHM_BUBBLESORT = 1;
const int SORT_ALGORITHM_QUICKSORT = 2;
const int SORT_ORDER_ASCENDING = 0;
const int SORT_ORDER_DESCENDING = 1;
void swap(int x[], int y[]) {
int t[5];
}
void selectionsort(int data[][5], int count, int column, int order) {
int i;
int j;
int k;
for (i = 0; i < count - 1; i++) {
j = i;
for (k = i + 1; k < count; k++) {
if (order == SORT_ORDER_ASCENDING) {
if (data[k][column] < data[j][column]) {
j = k;
}
} else {
if (data[j][column] < data[k][column]) {
j = k;
}
}
}
swap(data[i], data[j]);
}
}
void bubblesort(int data[][5], int count, int column, int order) {
int i;
int j;
for (i = count - 1; 0 < i; i--) {
for (j = 0; j < i; j++) {
if (order == SORT_ORDER_ASCENDING) {
if (data[j + 1][column] < data[j][column]) {
swap(data[j], data[j + 1]);
}
} else {
if (data[j][column] < data[j + 1][column]) {
swap(data[j], data[j + 1]);
}
}
}
}
}
void quicksort_(int data[][5], int left, int right, int column, int order) {
int lp;
int rp;
int pivot;
if (left >= right) {
return;
}
pivot = data[left][column];
lp = left;
rp = right;
while (lp <= rp) {
if (order == SORT_ORDER_ASCENDING) {
while (data[lp][column] < pivot) {
lp++;
}
} else {
while (pivot < data[lp][column]) {
lp++;
}
}
if (order == SORT_ORDER_ASCENDING) {
while (pivot < data[rp][column]) {
rp--;
}
} else {
while (data[rp][column] < pivot) {
rp--;
}
}
if (lp > rp) {
break;
}
swap(data[lp], data[rp]);
lp++;
rp--;
}
quicksort_(data, left, rp, column, order);
quicksort_(data, lp, right, column, order);
}
void quicksort(int data[][5], int count, int column, int order) {
quicksort_(data, 0, count - 1, column, order);
}
void sort(int data[][5], int count, int column, int algorithm, int order) {
if (0) {
} else if (algorithm == SORT_ALGORITHM_SELECTIONSORT) {
selectionsort(data, count, column, order);
} else if (algorithm == SORT_ALGORITHM_BUBBLESORT) {
bubblesort(data, count, column, order);
} else if (algorithm == SORT_ALGORITHM_QUICKSORT) {
quicksort(data, count, column, order);
}
}
int main(void) {
int data[5][5] = {{0}};
const int number = 0;
const int height = 1;
const int heightDiff = 2;
const int weight = 3;
const int weightDiff = 4;
const int count = 5;
int ave_heigh;
int ave_weight;
int sum;
int i;
/* input */
for (i = 0; i < 5; i++) {
scanf("%d,%d,%d", &data
[i
][number
], &data
[i
][height
], &data
[i
][weight
]); }
/* compute average height */
sum = 0;
for (i = 0; i < 5; i++) {
sum += data[i][height];
}
ave_heigh = sum / 5;
/* compute difference height */
for (i = 0; i < 5; i++) {
data[i][heightDiff] = data[i][height] - ave_heigh;
}
/* compute average weight */
sum = 0;
for (i = 0; i < 5; i++) {
sum += data[i][weight];
}
ave_weight = sum / 5;
/* compute difference weight */
for (i = 0; i < 5; i++) {
data[i][weightDiff] = data[i][weight] - ave_weight;
}
/* sort */
sort(data, count, SORT_COLUMN_NUMBER, SORT_ALGORITHM_SELECTIONSORT, SORT_ORDER_ASCENDING);
sort(data, count, SORT_COLUMN_HEIGHT, SORT_ALGORITHM_BUBBLESORT, SORT_ORDER_DESCENDING);
sort(data, count, SORT_COLUMN_WEIGHT, SORT_ALGORITHM_QUICKSORT, SORT_ORDER_ASCENDING);
/* output */
for (i = 0; i < count; i++) {
printf("%d, %d, %d, %d, %d\n", data
[i
][0], data
[i
][1], data
[i
][2], data
[i
][3], data
[i
][4]); }
printf("average height:%d\n", ave_heigh
); printf("average weight:%d\n", ave_weight
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtZW1vcnkuaD4KCmNvbnN0IGludCBTT1JUX0NPTFVNTl9OVU1CRVIgPSAwOwpjb25zdCBpbnQgU09SVF9DT0xVTU5fSEVJR0hUID0gMTsKY29uc3QgaW50IFNPUlRfQ09MVU1OX1dFSUdIVCA9IDM7Cgpjb25zdCBpbn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