-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy path2-D_MatrixSorting.cpp
79 lines (73 loc) · 1.71 KB
/
2-D_MatrixSorting.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Given a n x n matrix. The problem is to sort the given matrix in strict order.
* Here strict order means that matrix is sorted in a way such that all elements
* in a row are sorted in increasing order and for row ‘i’,
* where 1 <= i <= n-1, first element of row 'i' is greater than or equal to the last element of row 'i-1'.
* Input:
* First line of input contains dimension n of the matrix.
* Next n line form n rows with n elements each.
* Output:
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout << "Enter the size of an array : " << endl;
cin >> n;
int arr[1000][1000];
cout << "Enter the elements in an array : " << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> arr[i][j];
}
}
// Array to store all the elements in 1_D for sorting
int oneD_arr[n * n + 2];
int pos = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
oneD_arr[pos++] = arr[i][j];
}
}
// Using inbuilt sort
sort(oneD_arr, oneD_arr + pos);
pos = 0;
cout << "\n Sorted 2-D Matrix is:"
<< "\n";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
// changing value of intial array with sorted values
arr[i][j] = oneD_arr[pos++];
// displays output
cout << arr[i][j] << " ";
}
cout << "\n";
}
return (0);
}
/*
Time complexity : O(n)
Space complexity : O(n)
*/
/*
Test Case :
Input:
Enter the size of an array :
3
Enter the elements in an array :
5 4 7
1 3 8
2 9 6
Output:
Sorted 2-D Matrix is:
1 2 3
4 5 6
7 8 9
*/