forked from SakshiMishra1/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_sort.c
48 lines (37 loc) · 893 Bytes
/
matrix_sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
#define SIZE 10
void sortMatrix(int m[SIZE][SIZE], int n)
{
int temp[n * n];
int k = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
temp[k++] = m[i][j];
sort(temp, temp + k);
k = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
m[i][j] = temp[k++];
}
void printMatrix(int m[SIZE][SIZE], int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << m[i][j] << " ";
cout << endl;
}
}
int main()
{
int m[SIZE][SIZE] = { { 5, 4, 7 },
{ 1, 3, 8 },
{ 2, 9, 6 } };
int n = 3;
cout << "Original Matrix:\n";
printMatrix(m, n);
sortMatrix(m, n);
cout << "\nMatrix After Sorting:\n";
printMatrix(m, n);
return 0;
}