VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-to-sort-2d-array-across-rows/

⇱ C Program To Sort 2D Array Across Rows - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program To Sort 2D Array Across Rows

Last Updated : 23 Jul, 2025

Here, we will see how to sort the 2D Array across rows using a C program:

Input:

8 5 7 2
7 3 0 1
8 5 3 2
9 4 2 1

Output:

2 5 7 8
0 1 3 7
2 3 5 8
1 2 4 9

Approach: 

In this approach, we use Bubble Sort. First Start iterating through each row of the given 2D array, and sort elements of each row using the Bubble sort sorting algorithm. 

Below is the implementation of the above approach:


Output
 Input Array 
 8 5 7 2
 7 3 0 1
 8 5 3 2
 9 4 2 1

 Row-Wise Sorted 2D Array 
 2 5 7 8
 0 1 3 7
 2 3 5 8
 1 2 4 9

Time Complexity: O(r*c*max(r,c)).
Auxiliary Space: O(1).

Comment
Article Tags: