0
$\begingroup$

Hello I am a new Computer Science student
Ok, so I am trying to sort the 2D array I know you can actually sort a 2D array by copying it into a 1D array sort it using any Algorithm, and then place back into the 2D array but here is the problem aren't we using more space and time because we have to copy the array back. What I have tried so far

#include<stdio.h>

#include<stdlib.h>
#include<time.h>

#define ROWS 3
#define COL 5

void sort(int array[], int size);
void random(int rows, int col, int array[][col]);
void print(int rows, int col, int array[][col]);

int main(void)
{
    int set = ROWS*COL;
    int array[ROWS][COL];
    random(ROWS, COL, array);
    print(ROWS, COL, array);
    sort(array[0], set);
    printf("\n\n\n");
    print(ROWS, COL, array);
    return 0;
            
}
void print(int rows, int col, int array[][col])
{
    int idx, kdx;
    
    for(idx = 0; idx < rows; idx++)
    {
        for(kdx = 0; kdx < col; kdx++)
        {
            printf("%d ", array[idx][kdx]);
        }
        printf("\n");
    }
}
void random(int rows, int col, int array[][col])
{
    int idx, kdx;
    
    for(idx = 0; idx < rows; idx++)
    {
        for(kdx = 0; kdx < col; kdx++)
        {
            array[idx][kdx] = rand() % 10;
        }
    }
}
void sort(int array[], int size)
{
    int temp, idx, kdx;
    for(idx = 0; idx < size; idx++)
    {
        for(kdx = 0; kdx < size; kdx++)
        {
            if(array[kdx] > array[kdx+1])
            {
                temp = array[kdx + 1];
                array[kdx + 1] = array[kdx];
                array[kdx] = temp;
            }
        }
    }
}
 

But it's not any kind of algorithm and it will not work with dynamic memory you can say since it is just taking benefit of the way c store arrays. But things are different for dynamic arrays in c Is there any Algorithm to sort 2D array.

$\endgroup$
1
  • $\begingroup$ Welcome to COMPUTER SCIENCE @SE. What is your question, and what is the computer science angle? Have a(another) look at the hovers of tags c and c++ (rarely appropriate in combination, anyway): programming questions are off-topic. $\endgroup$ – greybeard Sep 28 '20 at 9:37
0
$\begingroup$

By dynamic I presume you mean either:

int* array = malloc(sizeof(int)*col*rows);

or

int* array = new int[rows][col]();

In which case its fairly simple:

void sort(int* array, int size)
{
    int temp, idx, kdx;
    for(idx = 0; idx < size; idx++)
    {
        for(kdx = 0; kdx < size; kdx++)
        {
            if(array[kdx] > array[kdx+1])
            {
                temp = array[kdx + 1];
                array[kdx + 1] = array[kdx];
                array[kdx] = temp;
            }
        }
    }
}

//in main or wherever
int* array = malloc(sizeof(int)*col*rows);
random(array, col*rows);
sort(array, col*rows);
free(array);

Although that is a very sloooooowwwwww sorting operation. How about researching logsort or quicksort?

$\endgroup$
1
  • $\begingroup$ This looks an off-topic answer to a question that may have a CS angle: How to define an order on a 2D matrix/array. While How do I write a Good Answer? does not repeat that a well-asked question is on-topic, it reminds of editing the question you're answering to improve the clarity and focus. $\endgroup$ – greybeard Sep 28 '20 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.