#include <stdio.h>
#include <stdlib.h>
#define COLS 4
int compare(const void* a, const void* b) {
return (*(int*)a - *(int*)b);
}
void sort_column(int matrix[][COLS], int rows, int col) {
int* parr = (int*)malloc(rows * sizeof(int));
for (int i = 0; i < rows; i++) {
parr[i] = matrix[i][col];
}
qsort(parr, rows, sizeof(int), compare);
for (int i = 0; i < rows; i++) {
matrix[i][col] = parr[i];
}
free(parr);
}
int main()
{
int matrix[][4] = {
{8, 3, 5, 1},
{4, 7, 6, 5},
{9, 1, 0, 4}
};
int rows = (sizeof(matrix) / sizeof(matrix[0]));
int cols = (sizeof(matrix) / sizeof(matrix[0][0])) / rows;
sort_column(matrix, rows, 1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%2d", matrix[i][j]);
}
printf("\n");
}
return 0;
}
/*
run:
8 1 5 1
4 3 6 5
9 7 0 4
*/