C Program to sort rows of the Matrix Last Updated : 10 Jan, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a matrix arr[][] of dimensions N * M, the task is to sort the matrix such that each row is sorted and the first element of each row is greater than or equal to the last element of the previous row. Examples: Input: N = 3, M = 3, arr[][] = {{7, 8, 9}, {5, 6, 4}, {3, 1, 2}}Output:1 2 34 5 67 8 9 Approach: Follow the steps below to solve the problem: Traverse the matrix For every matrix element, consider it to be the minimum element in the matrix. Check if there is a smaller element present in the rest of the matrix. If found to be true, swap the current element and the minimum element in the matrix. Finally, print the sorted matrix. Below is the implementation for the above approach: C // C program for the above approach #include <stdio.h> #include <stdlib.h> #define SIZE 100 // Function to sort a matrix void sort_matrix(int arr[SIZE][SIZE], int N, int M) { // Traverse over the matrix for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { // Current minimum element int minimum = arr[i][j]; // Index of the current // minimum element int z = i; int q = j; // Check if any smaller element // is present in the matrix int w = j; for (int k = i; k < N; k++) { for (; w < M; w++) { // Update the minimum element if (arr[k][w] < minimum) { minimum = arr[k][w]; // Update the index of // the minimum element z = k; q = w; } } w = 0; } // Swap the current element // and the minimum element int temp = arr[i][j]; arr[i][j] = arr[z][q]; arr[z][q] = temp; } } } // Function to print the sorted matrix void printMat(int arr[SIZE][SIZE], int N, int M) { for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { printf("%d ", arr[i][j]); } printf("\n"); } } // Driver Code int main() { int N = 3, M = 3; int arr[SIZE][SIZE] = { { 7, 8, 9 }, { 5, 6, 4 }, { 3, 1, 2 } }; // Sort the matrix sort_matrix(arr, N, M); // Print the sorted matrix printMat(arr, N, M); return 0; } Output: 1 2 3 4 5 6 7 8 9 Time Complexity: O(N4)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article C Program to sort rows of the Matrix N narayandwivedi21 Follow Improve Article Tags : C Language Similar Reads C Program for Selection Sort The selection sort is a simple comparison-based sorting algorithm that sorts a collection by repeatedly finding the minimum (or maximum) element and placing it in its correct position in the list. It is very simple to implement and is preferred when you have to manually implement the sorting algorit 4 min read C program to store Student records as Structures and Sort them by Age or ID Given studentâs records with each record containing id, name and age of a student. Write a C program to read these records and display them in sorted order by age or id. Sorting by Age Examples: Input: Student Records = { {Id = 1, Name = bd, Age = 12 }, {Id = 2, Name = ba, Age = 10 }, {Id = 3, Name 6 min read Sort Vector of Pairs in descending order in C++ We have discussed some of the cases of sorting vector of pairs in below set 1. Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second) More cases are discussed in this article. Sometimes we require to sort the vector in reverse order. In those instances, rather than first sorting the vecto 5 min read Sorting 2D Vector in C++ | Set 2 (In descending order by row and column) We have discussed some of the cases of sorting 2D vector in below set 1. Sorting 2D Vector in C++ | Set 1 (By row and column) More cases are discussed in this article Case 3 : To sort a particular row of 2D vector in descending order This type of sorting arranges a selected row of 2D vector in desce 4 min read Sorting 2D Vector in C++ | Set 3 (By number of columns) We have discussed some of the cases of sorting 2D vector in below set 1 and set 2.Sorting 2D Vector in C++ | Set 1 (By row and column) Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)More cases are discussed in this articleAs mentioned in one of the article published of this 4 min read Like