二维数组升序排序功能失效,寻求技术解决方案
Hey there! Let's get your 2D array sorted in ascending order. I see you've already got the input and basic output working—great start! The key thing to remember is that 2D arrays in C are stored contiguously in memory, which means we can treat them like a single 1D array for sorting, or use nested loops to compare every element directly. Let's walk through both approaches with complete code.
Approach 1: Treat 2D Array as 1D (Simpler Bubble Sort)
This method leverages how C stores 2D arrays (row-by-row in a single block of memory). We can use standard bubble sort logic, converting 1D indices to 2D row/column positions when accessing elements.
Complete Code
#include <stdio.h> int main() { // 2D array declaration (2 rows, 3 columns) int Array[2][3]; printf("***** Bubble Sort Assessment 2 *****\n"); int i, j, temp; const int totalElements = 2 * 3; // Total elements in the array const int columns = 3; // Get user input printf("Enter %d elements:\n", totalElements); for(i = 0; i < 2; i++) { for(j = 0; j < 3; j++) { scanf("%d", &Array[i][j]); } } // Print original array printf("\nOriginal array:\n"); for(i = 0; i < 2; i++) { for(j = 0; j < 3; j++) { printf("%d ", Array[i][j]); } printf("\n"); } // Bubble sort logic (treat 2D array as 1D) for(i = 0; i < totalElements - 1; i++) { for(j = 0; j < totalElements - i - 1; j++) { // Convert 1D index to 2D coordinates int row1 = j / columns; int col1 = j % columns; int row2 = (j + 1) / columns; int col2 = (j + 1) % columns; // Swap if current element is larger than next if(Array[row1][col1] > Array[row2][col2]) { temp = Array[row1][col1]; Array[row1][col1] = Array[row2][col2]; Array[row2][col2] = temp; } } } // Print sorted array printf("\nSorted array (ascending order):\n"); for(i = 0; i < 2; i++) { for(j = 0; j < 3; j++) { printf("%d ", Array[i][j]); } printf("\n"); } return 0; }
Key Explanations
- Memory Contiguity: A
2x3array is stored as[0][0], [0][1], [0][2], [1][0], [1][1], [1][2]in memory—so we can loop through all elements with a single 1D index. - Index Conversion: For any 1D index
j, the row isj / number_of_columnsand the column isj % number_of_columns. This maps the 1D position back to the 2D array.
Approach 2: Nested Loop Comparison (More Intuitive)
If you prefer a more explicit approach without relying on memory layout, you can compare every element with all elements that come after it, swapping when needed.
Modified Sorting Code Snippet
Replace the bubble sort section in the code above with this:
// Alternative bubble sort (explicit 2D comparisons) for(i = 0; i < 2; i++) { for(j = 0; j < 3; j++) { int k, l; // Compare current element with all elements after it for(k = i; k < 2; k++) { // Start from j+1 if we're in the same row, else start from column 0 int startCol = (k == i) ? j + 1 : 0; for(l = startCol; l < 3; l++) { if(Array[i][j] > Array[k][l]) { // Swap elements temp = Array[i][j]; Array[i][j] = Array[k][l]; Array[k][l] = temp; } } } } }
Key Explanations
- We iterate over every element
Array[i][j], then compare it to every element that comes after it (Array[k][l]wherek >= iandl > jifk == i). - This is straightforward to follow, especially if you're still getting comfortable with how 2D arrays work under the hood.
Either approach will sort your 2D array in ascending order—pick the one that makes the most sense to you!
内容的提问来源于stack exchange,提问作者Morph

