Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
C Program to Sort a String of Characters
Next article icon

C Program To Sort 2D Array Across Rows

Last Updated : 02 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

C




// C program to sort 2D array row-wise
#include <stdio.h>
  
// This function sort 2D array row-wise
void sortRowWise(int m[][4], int r, int c)
{
    int t = 0;
    
    // loop for rows of 2d array
    for (int i = 0; i < r; i++) {
        
        // loop for column of 2d array
        for (int j = 0; j < c; j++) {
            
            // loop for comparison and swapping the elements
            for (int k = 0; k < c - j - 1; k++) {
                
                if (m[i][k] > m[i][k + 1]) {
                    
                    // swap the elements
                    t = m[i][k];
                    m[i][k] = m[i][k + 1];
                    m[i][k + 1] = t;
                }
            }
        }
    }
  
    // print the sorted matrix
    printf("\n Row-Wise Sorted 2D Array \n");
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++)
            printf(" %d", m[i][j]);
        printf("\n");
    }
}
  
// Driver code
int main()
{
    // Input Array
    int arr[][4] = { { 8, 5, 7, 2 },
                     { 7, 3, 0, 1 },
                     { 8, 5, 3, 2 },
                     { 9, 4, 2, 1 } };
  
    // print input array
    printf("\n Input Array \n");
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            printf(" %d", arr[i][j]);
        }
        printf("\n");
    }
  
    // sortRowWise(arr, 4, 4) accepts 2D-array, no of rows
    // and columns as input and print a row-wise sorted 2D
    // array
    sortRowWise(arr, 4, 4);
    return 0;
}
 
 
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).



Next Article
C Program to Sort a String of Characters
author
mukulsomukesh
Improve
Article Tags :
  • C Language
  • C Programs
  • C Array Programs

Similar Reads

  • C program to sort an array using pointers
    Given an array of size n, the task is to sort this array using pointers in C. Examples: Input: n = 5, arr[] = {0, 23, 14, 12, 9} Output: {0, 9, 12, 14, 23} Input: n = 3, arr[] = {7, 0, 2} Output: {0, 2, 7} Approach: The array can be fetched with the help of pointers with the pointer variable pointin
    2 min read
  • C Program to Traverse an Array
    Write a C program to traverse the given array that contains N number of elements. Examples Input: arr[] = {2, -1, 5, 6, 0, -3} Output: 2 -1 5 6 0 -3 Input: arr[] = {4, 0, -2, -9, -7, 1} Output: 4 0 -2 -9 -7 1 Different Ways to Traverse an Array in CArrays are versatile data structures and C language
    3 min read
  • C Program to Sort an Array in Ascending Order
    Sorting an array in ascending order means arranging the elements in the order from smallest element to largest element. The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array. Let's look at a simple example:
    3 min read
  • C Program to Sort a String of Characters
    Sorting a string of characters refers to the process of rearranging all the characters in the given order. In this article, we will learn how to sort a string of characters using the C program. The most straightforward method to sort a string of characters is by using the qsort() function. Let's tak
    3 min read
  • C Program For Radix Sort
    Radix Sort is a linear sorting algorithm that sorts elements by processing them digit by digit. It is an efficient sorting algorithm for integers or strings with fixed-size keys. Rather than comparing elements directly, Radix Sort distributes the elements into buckets based on each digit’s value. By
    3 min read
  • C Program For Sorting An Array Of 0s, 1s and 2s
    Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples: Input: {0, 1, 2, 0, 1, 2} Output: {0, 0, 1, 1, 2, 2} Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} Output: {0, 0, 0,
    4 min read
  • Array C/C++ Programs
    C Program to find sum of elements in a given arrayC program to find largest element in an arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum as xC/C++ Program for Majority ElementC/C++ Program for Find the Number Occurring Odd N
    6 min read
  • How to Initialize a 2D Array in C?
    In C, a 2D Array is a type of multidimensional array in which data is stored in tabular form (rows and columns). It has two dimensions so it can store the data and can expand in two directions. In this article, we will learn how to initialize a 2D array in C. There are three main ways to initialize
    4 min read
  • C Program for Heap Sort
    Heap sort is a comparison-based sorting technique. It works on the data structure known as "the binary heap". It is one of the most efficient sorting algorithms. It takes relatively less time than selection sort, bubble sort, and insertion sort. Heap sort is an unstable sorting algorithm since the p
    4 min read
  • How to Pass a 3D Array to a Function in C?
    A 3D array (or three-dimensional array) in C is a multi-dimensional array that contains multiple layers of two-dimensional arrays stacked on top of each other. It stores elements that can be accessed using three indices: the depth index, row index, and column index. In this article, we will learn ho
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences