|
| 1 | +// C++ program to Convert given Matrix |
| 2 | +// into sorted Spiral Matrix |
| 3 | +#include <bits/stdc++.h> |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +const int MAX = 1000; |
| 7 | + |
| 8 | +// Function to convert the array to Spiral |
| 9 | +void ToSpiral(int m, int n, |
| 10 | +int Sorted[], int a[MAX][MAX]) |
| 11 | +{ |
| 12 | +// For Array pointer |
| 13 | +int index = 0; |
| 14 | + |
| 15 | +// k - starting row index |
| 16 | +// m - ending row index |
| 17 | +// l - starting column index |
| 18 | +// n - ending column index |
| 19 | +int k = 0, l = 0; |
| 20 | + |
| 21 | +while (k < m && l < n) |
| 22 | +{ |
| 23 | + |
| 24 | +// Print the first row |
| 25 | +// from the remaining rows |
| 26 | +for (int i = l; i < n; ++i) |
| 27 | +{ |
| 28 | +a[k][i] = Sorted[index]; |
| 29 | +index++; |
| 30 | +} |
| 31 | + |
| 32 | +k++; |
| 33 | + |
| 34 | +// Print the last column |
| 35 | +// from the remaining columns |
| 36 | +for (int i = k; i < m; ++i) |
| 37 | +{ |
| 38 | +a[i][n - 1] = Sorted[index]; |
| 39 | +index++; |
| 40 | +} |
| 41 | +n--; |
| 42 | + |
| 43 | +// Print the last row |
| 44 | +// from the remaining rows |
| 45 | +if (k < m) |
| 46 | +{ |
| 47 | +for (int i = n - 1; i >= l; --i) |
| 48 | +{ |
| 49 | +a[m - 1][i] = Sorted[index]; |
| 50 | +index++; |
| 51 | +} |
| 52 | +m--; |
| 53 | +} |
| 54 | + |
| 55 | +// Print the first column |
| 56 | +// from the remaining columns |
| 57 | +if (l < n) |
| 58 | +{ |
| 59 | +for (int i = m - 1; i >= k; --i) |
| 60 | +{ |
| 61 | +a[i][l] = Sorted[index]; |
| 62 | +index++; |
| 63 | +} |
| 64 | +l++; |
| 65 | +} |
| 66 | +} |
| 67 | +} |
| 68 | + |
| 69 | +// Function to convert 2D array to 1D array |
| 70 | +void convert2Dto1D(int y[MAX][MAX], |
| 71 | +int m, int n,int x[]) |
| 72 | +{ |
| 73 | + |
| 74 | +int index = 0; |
| 75 | + |
| 76 | +// Store value 2D Matrix To 1D array |
| 77 | +for (int i = 0; i < m; i++) |
| 78 | +{ |
| 79 | +for (int j = 0; j < n; j++) |
| 80 | +{ |
| 81 | +x[index] = y[i][j]; |
| 82 | +index++; |
| 83 | +} |
| 84 | +} |
| 85 | +} |
| 86 | + |
| 87 | +// Function to print the Matrix |
| 88 | +void PrintMatrix(int a[MAX][MAX], |
| 89 | +int m, int n) |
| 90 | +{ |
| 91 | + |
| 92 | +// Print Spiral Matrix |
| 93 | +for (int i = 0; i < m; i++) |
| 94 | +{ |
| 95 | +for (int j = 0; j < n; j++) |
| 96 | +{ |
| 97 | +cout << a[i][j] << " "; |
| 98 | +} |
| 99 | +cout << endl; |
| 100 | +} |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +// Function to Convert given Matrix |
| 105 | +// into sorted Spiral Matrix |
| 106 | +void convertMatrixToSortedSpiral( |
| 107 | +int y[MAX][MAX], int m, int n) |
| 108 | +{ |
| 109 | +int a[MAX][MAX] = {0}; |
| 110 | +int x[m * n]; |
| 111 | + |
| 112 | +convert2Dto1D(y, m, n,x); |
| 113 | +sort(x, x + n * m); |
| 114 | +ToSpiral(m, n, x, a); |
| 115 | +PrintMatrix(a, m, n); |
| 116 | +} |
| 117 | + |
| 118 | +// Driver code |
| 119 | +int main() |
| 120 | +{ |
| 121 | +int m = 4, n = 3; |
| 122 | +int y[MAX][MAX] = { |
| 123 | +{ 2, 5, 12 }, |
| 124 | +{ 22, 45, 55 }, |
| 125 | +{ 1, 6, 8 }, |
| 126 | +{ 13, 56, 10 }}; |
| 127 | + |
| 128 | +convertMatrixToSortedSpiral(y, m, n); |
| 129 | + |
| 130 | +return 0; |
| 131 | +} |
| 132 | + |
0 commit comments