I've just started to learn C. And I'm trying to sort the columns in 2 dimensional arrays in C.
Here's the problem
Write a program to input an array of m x n. Sort the odd column in increasing order and the even column in decreasing order
My idea is to use recursive function. At each row of the array, I will push the elements in a new array, sort it, and reassign to the old array. Loop that recursively until we reach the end of the columns.
Here's my code in C:
#include <stdio.h>
int j = 0, temp[] = {}, sizeTemp = 0; // The size of temp, change overtime in loop()
int arr[4][3] = {
{1,2,3},
{4,8,6},
{2,1,5},
{3,7,4}
};
void pushToArray(int arr[], int currentEl, int addEl){ // Push an element into an array
arr[currentEl] = addEl;
}
void bubbleSort(int arr[], int size, int inc_dec){
// Using bubble sort to sort the temporary array, which is then reassigned to the old array
int temp;
for(int i = 0; i < size; i++){
for(int j = 0; j < size - 1;j++){
if(inc_dec == 1){
// Increasing
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}else{
// Decreasing
if(arr[j] < arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
int justForReturnSomething(){
// void function can't return a value, so I use this just for returning something
return 1;
}
void loop(){
int temp2;
for(int i = 0; i < 4; i++){
if(j > 2){
return justForReturnSomething();
}
temp2 = arr[i][j];
pushToArray(temp,sizeTemp,temp2);
sizeTemp++;
if(j % 2 != 0){
bubbleSort(temp,sizeTemp,1);
}else{
bubbleSort(temp, sizeTemp,-1);
}
}
for(int k = 0; k < 4; k++){
arr[k][j] = temp[k];
}
if(j > 2){
return justForReturnSomething();
}else{
j++;
for(int m = 0; m < sizeTemp; m++){ // Reassign the temp to empty
temp[m] = 0;
}
sizeTemp = 0; // reassign the size of temp to 0
loop();
}
}
int main(){
loop();
printf("Your sorted array: \n");
for(int i = 0; i < 4; i++){
for(int j = 0; j < 3; j++){
printf("%d,",arr[i][j]);
}
printf("\n");
}
return 0;
}
Result:
Your sorted array:
5,0,7,
4,0,6,
3,0,4,
Just for DEMONSTRATING the idea to approach the problem, I tried it with Javasript, the result is fine:
let arr = [
[1,2,3],
[4,8,6],
[2,1,5],
[3,7,4]
];
let temp = [], j=0;
function bubbleSort( arr, size, inc_dec){
let temp;
for(let i = 0; i < size; i++){
for(let j = 0; j < size - 1;j++){
if(inc_dec == 1){
// Increasing
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}else{
// Decreasing
if(arr[j] < arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
function loop(){
for(let i = 0; i < 4; i++){
if(j > 2){
return;
}
temp.push(arr[i][j]);
if(j % 2 !== 0){
bubbleSort(temp,temp.length,1);
}else{
bubbleSort(temp, temp.length,-1);
}
}
for(let k = 0; k < 4; k++){
arr[k][j] = temp[k];
}
if(j > 2){
return;
}else{
j++;
temp = [];
loop();
}
}
loop();
console.log(arr);
Since I've tried to solve this with my own idea and did come up with a solution without searching on google, I'd be very happy if you can guide me to bring in the solution for C with this approach. But, other approaches are greatly appreciated.


function sortColumn(matrix, columnIndex, desc) { let sortFn = desc ? (a, b) => b - a : (a, b) => a - b; matrix .map(row => row[columnIndex]) .sort(sortFn) .forEach((columnValue, rowIndex) => matrix[rowIndex][columnIndex] = columnValue); return matrix; }