in my problem i have to enter the components of matrix in 2D array
i.e

input:
1 5 6
3 9 10
8 23 15


output:
1 3 5
6 8 9
0 15 23

Dani AI

Generated

The example from looks like the intent is to take all entries of an n x n matrix, sort them in ascending order, and refill the matrix row-by-row (left-to-right, top-to-bottom). The posted output contains a likely typo — the 0 in the bottom-left should be 10 if you sort all nine values.

Both approaches mentioned earlier have merit: flattening the matrix into a single list, sorting that list, then copying it back is simple and fast; doing an in-place selection-style pass avoids extra memory but is much slower. Building on and , a practical and robust C pattern is to treat the matrix as a contiguous row-major block, use the C library sort for speed, and copy back. The snippet below shows a safe comparator, a flatten-copy, qsort usage, and copying back into the original block (no malloc cast; check allocation):

#include <stdio.h>
#include <stdlib.h>

int cmp_int(const void *a, const void *b) {
    int va = *(const int *)a;
    int vb = *(const int *)b;
    return (va > vb) - (va < vb);
}

/* data points to the first element of a row-major n-by-n matrix */
void sort_square(int *data, size_t n) {
    size_t total = n * n;
    int *flat = malloc(total * sizeof *flat);
    if (!flat) {
        perror("malloc");
        return;
    }
    for (size_t i = 0; i < total; ++i) flat[i] = data[i];
    qsort(flat, total, sizeof *flat, cmp_int);
    for (size_t i = 0; i < total; ++i) data[i] = flat[i];
    free(flat);
}

Notes and troubleshooting: always verify you pass a contiguous block (for a VLA or declared as int a[n][n] pass &a[0][0]); handle allocation failure; be mindful of integer overflow when computing n * n for very large n; if you need column-wise filling instead, change the copy-back order; if stability matters (preserve equal-element order) use a stable sort implementation. If memory is extremely constrained, do repeated-min extraction directly on the 2D indices, but expect quadratic work over n^2 elements (slow for large n).

Recommended Answers

All 2 Replies

Well there are several methods , but I would go with the simplest...

Assume that you take the dimension of nxn martix as the input from the user where user feeds in value of n. And array[][] is the one in which he feeds in the value.

Just have another integer pointer as

int *ptr;

After the user feeds in the value of n you can allocate a linear array as

ptr=(int *)malloc(n*n*sizeof(int));

Use this array to sort your elements out using the pointer itself as index to the array as:

*(ptr+i)

or even as:

ptr[i]

Once you are done wit sorting then you can just free the memory as

free(ptr);

This would be just like using a temporary array for sorting.

Hope this helps... :)

ptr=(int *)malloc(n*n*sizeof(int));

Casting malloc is a residue practice from the times when the C programming language didn't support void type. It is best not to cast malloc since doing it can hide errors. #include <stdlib.h>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.