#include <iostream>
#include <algorithm>
using namespace std;
void exchange(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
void sortMatrix(int N, int M, int arr[][100]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N - i - 1; j++) {
if (arr[j][M - 1] < arr[j + 1][M - 1]) {
for (int k = 0; k < M; k++) {
exchange(arr[j][k], arr[j + 1][k]);
int main() {
int N, M;
cin >> N >> M;
int arr[100][100];
for (int j = 0; j < M; j++) {
cin >> arr[i][j];
sortMatrix(N, M, arr);
cout << arr[i][j] << " ";
cout << endl;
return 0;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
#include <iostream>
#include <algorithm>
using namespace std;
void exchange(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
void sortMatrix(int N, int M, int arr[][100]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N - i - 1; j++) {
if (arr[j][M - 1] < arr[j + 1][M - 1]) {
for (int k = 0; k < M; k++) {
exchange(arr[j][k], arr[j + 1][k]);
}
}
}
}
}
int main() {
int N, M;
cin >> N >> M;
int arr[100][100];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> arr[i][j];
}
}
sortMatrix(N, M, arr);
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}