C++ Program to Sort the given matrix Last Updated : 18 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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'.Examples: Input : mat[][] = { {5, 4, 7}, {1, 3, 8}, {2, 9, 6} } Output : 1 2 3 4 5 6 7 8 9 Approach: Create a temp[] array of size n^2. Starting with the first row one by one copy the elements of the given matrix into temp[]. Sort temp[]. Now one by one copy the elements of temp[] back to the given matrix. C++ // C++ implementation to sort the given matrix #include <bits/stdc++.h> using namespace std; #define SIZE 10 // function to sort the given matrix void sortMat(int mat[SIZE][SIZE], int n) { // temporary matrix of size n^2 int temp[n * n]; int k = 0; // copy the elements of matrix one by one // into temp[] for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) temp[k++] = mat[i][j]; // sort temp[] sort(temp, temp + k); // copy the elements of temp[] one by one // in mat[][] k = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mat[i][j] = temp[k++]; } // function to print the given matrix void printMat(int mat[SIZE][SIZE], int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << mat[i][j] << " "; cout << endl; } } // Driver program to test above int main() { int mat[SIZE][SIZE] = { { 5, 4, 7 }, { 1, 3, 8 }, { 2, 9, 6 } }; int n = 3; cout << "Original Matrix: "; printMat(mat, n); sortMat(mat, n); cout << " Matrix After Sorting: "; printMat(mat, n); return 0; } Output: Original Matrix: 5 4 7 1 3 8 2 9 6 Matrix After Sorting: 1 2 3 4 5 6 7 8 9 Time Complexity: O(n2log2n). Auxiliary Space: O(n2). Please refer complete article on Sort the given matrix for more details! Comment More infoAdvertise with us Next Article C++ Program to Sort the given matrix kartik Follow Improve Article Tags : Sorting Matrix C++ Programs C++ DSA +1 More Practice Tags : CPPMatrixSorting Similar Reads C++ Program to Rotate a Matrix by 180 degree Given a square matrix, the task is that we turn it by 180 degrees in an anti-clockwise direction without using any extra space. Examples : Input : 1 2 3 4 5 6 7 8 9 Output : 9 8 7 6 5 4 3 2 1 Input : 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 Output : 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 Method: 1 (Only prints ro 6 min read C++ Program to Sort the matrix row-wise and column-wise Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.Examples:  Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 3 min read C++ Program to Find median in row wise sorted matrix We are given a row-wise sorted matrix of size r*c, we need to find the median of the matrix given. It is assumed that r*c is always odd.Examples: Input : 1 3 5 2 6 9 3 6 9 Output : Median is 5 If we put all the values in a sorted array A[] = 1 2 3 3 5 6 6 9 9) Input: 1 3 4 2 5 6 7 8 9 Output: Media 4 min read C++ Program to Sort String of Characters Sorting a string means rearranging the characters of the given string in some defined order such as alphabetical order. In this article, we will learn how to sort a string by characters in C++.ExamplesInput: str = "geeksforgeeks"Output: "eeeefggkkorss"Explanation: The characters in the string are so 4 min read C++ Program for Identity Matrix Introduction to Identity Matrix : The dictionary definition of an Identity Matrix is a square matrix in which all the elements of the principal or main diagonal are 1's and all other elements are zeros. In the below image, every matrix is an Identity Matrix. In linear algebra, this is sometimes call 3 min read Sort the matrix column-wise Given a matrix mat[][] of dimensions N * M, the task is to sort each column of a matrix in ascending order and print the updated matrix. Examples: Input: mat[][] = { {1, 6, 10}, {8, 5, 9}, {9, 4, 15}, {7, 3, 60} }Output:1 3 97 4 108 5 159 6 60Explanation:The input matrix is sorted in a column wise m 8 min read C++ Program For Insertion Sort Insertion sort is a simple sorting algorithm that works by dividing the array into two parts, sorted and unsorted part. In each iteration, the first element from the unsorted subarray is taken and it is placed at its correct position in the sorted array. In this article, we will learn to write a C++ 3 min read C++ Program to Print matrix in zag-zag fashion Given a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure. Example: Input: 1 2 3 4 5 6 7 8 9 Output: 1 2 4 7 5 3 6 8 9 Approach of C++ code The approach is simple. Just simply iterate over every diagonal elements one at a time and change the directio 3 min read C++ Program to Sort the Elements of an Array in Ascending Order Here, we will see how to sort the elements of an array in ascending order using a C++ program. Below are the examples: Input: 3 4 5 8 1 10Output: 1 3 4 5 8 10 Input: 11 34 6 20 40 3Output: 3 6 11 20 34 40 There are 2 ways to sort an array in ascending order in C++: Brute-force Approach Using Bubble 4 min read C++ Program for Bitonic Sort Bitonic Sequence A sequence is called Bitonic if it is first increasing, then decreasing. In other words, an array arr[0..n-i] is Bitonic if there exists an index i where 0<=i<=n-1 such that x0 <= x1 â¦..<= xi and xi >= xi+1â¦.. >= xn-1 A sequence, sorted in increasing order is consi 4 min read Like