Print index of columns sorted by count of zeroes in the Given Matrix in C Program.

Given a matrix of size NxM where N is the number of rows and M is the number of columns, we need to print the column indices sorted by the count of zeros in each column. This means we first count zeros in each column, then sort the columns based on these counts, and finally print the column indices (1-based indexing).

For example, if column 1 contains 1 zero, column 2 contains 0 zeros, and column 3 contains 2 zeros, the result should be − 2 1 3 (sorted by zero count: 0, 1, 2).

Syntax

void sortColumnsByZeroCount(int matrix[ROWS][COLS], int rows, int cols);

Algorithm

  1. Count zeros in each column of the matrix
  2. Store column index and zero count as pairs
  3. Sort the pairs based on zero count
  4. Print the column indices in sorted order

Example

Matrix (3x3): 0 0 0 1 1 1 1 0 1 Col 1 Col 2 Col 3 1 zero 2 zeros 0 zeros Sorted by zero count: Column 3 (0 zeros) ? Column 1 (1 zero) ? Column 2 (2 zeros) Output: 3 1 2
#include <stdio.h>
#include <stdlib.h>

#define ROWS 3
#define COLS 3

typedef struct {
    int zeroCount;
    int columnIndex;
} ColumnData;

int compare(const void *a, const void *b) {
    ColumnData *col1 = (ColumnData *)a;
    ColumnData *col2 = (ColumnData *)b;
    return col1->zeroCount - col2->zeroCount;
}

void sortColumnsByZeroCount(int matrix[ROWS][COLS]) {
    ColumnData columns[COLS];
    
    /* Count zeros in each column */
    for (int col = 0; col < COLS; col++) {
        int zeroCount = 0;
        for (int row = 0; row < ROWS; row++) {
            if (matrix[row][col] == 0) {
                zeroCount++;
            }
        }
        columns[col].zeroCount = zeroCount;
        columns[col].columnIndex = col + 1; /* 1-based indexing */
    }
    
    /* Sort columns by zero count */
    qsort(columns, COLS, sizeof(ColumnData), compare);
    
    /* Print sorted column indices */
    printf("Column indices sorted by zero count: ");
    for (int i = 0; i < COLS; i++) {
        printf("%d ", columns[i].columnIndex);
    }
    printf("<br>");
}

int main() {
    int matrix[ROWS][COLS] = {
        {0, 0, 0},
        {1, 1, 1},
        {1, 0, 1}
    };
    
    printf("Matrix:<br>");
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("<br>");
    }
    
    sortColumnsByZeroCount(matrix);
    return 0;
}
Matrix:
0 0 0 
1 1 1 
1 0 1 
Column indices sorted by zero count: 3 1 2

How It Works

  • Column 1: Contains 1 zero (at position [0][0])
  • Column 2: Contains 2 zeros (at positions [0][1] and [2][1])
  • Column 3: Contains 1 zero (at position [0][2])

After sorting by zero count: Column 3 (1 zero), Column 1 (1 zero), Column 2 (2 zeros). Since columns 1 and 3 have the same zero count, their relative order is maintained.

Key Points

  • We use a structure to pair zero count with column index for easier sorting
  • The qsort() function is used for sorting with a custom comparator
  • Column indices are printed in 1-based format as required
  • Time complexity is O(n*m + m log m) where n is rows and m is columns

Conclusion

This program efficiently sorts matrix columns by their zero count using a structure-based approach with qsort(). The solution handles the sorting logic cleanly and provides 1-based column indexing in the output.

Updated on: 2026-03-15T11:51:08+05:30

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements