Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My idea is that a 2D array can (in some cases) be considered as a 1D array because of the memory storage of it. If not, you can either copy it into a 1D array of write a custom sort function that use a function that translate the indexes from 1D to 2D.</p> <p>Here an example using the function qsort:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int matrix[4][4]; void print_matrix() { int i, j; for (i = 0; i &lt; 4; i++) { for (j = 0; j &lt; 4; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } int compar(const void *a, const void *b) { int ia = *((int*)a); int ib = *((int*)b); return ia - ib; } int main() { int i, j; // Init of a 2D array with random numbers: for (i = 0; i &lt; 4; i++) { for (j = 0; j &lt; 4; j++) { matrix[i][j] = random() % 10; } } // Before: printf("Before:\n"); print_matrix(); // This array can be considered as a big 1D array. qsort(matrix, 16, sizeof(int), compar); // After: printf("After:\n"); print_matrix(); return 0; } </code></pre> <p>Output:</p> <pre><code>Before: 3 6 7 5 3 5 6 2 9 1 2 7 0 9 3 6 After: 0 1 2 2 3 3 3 5 5 6 6 6 7 7 9 9 </code></pre> <p><strong>Edit:</strong> OP asked me to avoid using qsort... So here a quicksort able to sort a 2D array:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; void print_matrix(int **matrix, int rows, int cols) { int i, j; for (i = 0; i &lt; rows; i++) { for (j = 0; j &lt; cols; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } } void swap(int *a, int *b) { int buf = *a; *a = *b; *b = buf; } int partition(int **a, int l, int r, int c) { int i; // Left pivot int pivot_val = a[l/c][l%c]; // Move pivot to end swap(&amp;a[l/c][l%c], &amp;a[r/c][r%c]); // If &lt;= to the pivot value, swap int j = l; for (i = l; i &lt; r; i++) { if (a[i/c][i%c] &lt;= pivot_val) { swap(&amp;a[i/c][i%c], &amp;a[j/c][j%c]); j++; } } // Move pivot to its place. swap(&amp;a[j/c][j%c], &amp;a[r/c][r%c]); return j; } void quicksort_r(int **a, int l, int r, int c) { if (l &lt; r) { int pivot = partition(a, l, r, c); quicksort_r(a, l, pivot-1, c); quicksort_r(a, pivot+1, r, c); } } void quicksort(int **a, int rows, int cols) { quicksort_r(a, 0, rows * cols - 1, cols); } int main() { int i, j; int rows = 5; int cols = 4; int **matrix = malloc(sizeof(int*) * rows); // Init of a 2D array with random numbers: for (i = 0; i &lt; rows; i++) { matrix[i] = malloc(sizeof(int) * cols); for (j = 0; j &lt; cols; j++) { matrix[i][j] = random() % 10; } } // Before: printf("Before:\n"); print_matrix(matrix, rows, cols); quicksort(matrix, rows, cols); // After: printf("After:\n"); print_matrix(matrix, rows, cols); return 0; } </code></pre> <p>Which gives:</p> <pre><code>Before: 3 6 7 5 3 5 6 2 9 1 2 7 0 9 3 6 0 6 2 6 After: 0 0 1 2 2 2 3 3 3 5 5 6 6 6 6 6 7 7 9 9 </code></pre> <p><strong>Edit2:</strong> I realized afterward that there is another obvious solution for square matrices:</p> <p>Let's take the first example:</p> <pre><code>0 1 2 2 3 3 3 5 5 6 6 6 7 7 9 9 </code></pre> <p>There is also:</p> <pre><code>0 3 5 7 1 3 6 7 2 3 6 9 2 5 6 9 </code></pre> <p>But for the second example too:</p> <pre><code>0 0 1 2 2 2 3 3 3 5 5 6 6 6 6 6 7 7 9 9 </code></pre> <p>And:</p> <pre><code>0 2 3 6 0 2 5 6 1 3 5 6 2 3 6 6 7 7 9 9 </code></pre> <p>Which means that maybe we could do a specialized algorithm able to give all the solutions or an algorithm that tries to minimize the number of moves, I don't know. It's a quite interesting problem in fact.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload