C++动态二维数组行排序问题:基于正偶数和的冒泡排序失效
Hey there! Let's work through why your bubble sort isn't correctly sorting the rows of your dynamic 2D array. I've spotted two critical issues in your sorting logic, plus a minor memory cleanup point to fix.
Key Issues in Your Original Code
1. Broken Row Swap Logic
Your current code uses an extra loop over columns and swaps single elements (only the j-th element of rows k and k+1) instead of swapping entire rows. That's why rows are getting partially mixed up instead of being fully reordered.
2. Misaligned Sum Array
When you swap rows, you didn't update the sumRows array to match the new row order. This could cause inconsistencies if you need to use the sums later.
3. Unfreed Memory
You allocated sumRows with new int[rows] but never deleted it, leading to a small memory leak.
Corrected Code for the Sorting Section
Here's the fixed bubble sort logic that swaps entire rows (since your array is an int** pointer array, we can swap row pointers directly for efficiency):
// Inside your print function, replace the faulty sorting block with this: bool swapped; for (int i = 0; i < rows - 1; i++) { swapped = false; // Standard bubble sort iteration over rows for (int k = 0; k < rows - i - 1; k++) { if (sumRows[k] > sumRows[k + 1]) { // Swap entire rows by swapping their pointers swap(arr[k], arr[k + 1]); // Swap the corresponding sums to keep them aligned with the rows swap(sumRows[k], sumRows[k + 1]); swapped = true; } } if (!swapped) break; // Exit early if no swaps occurred (array is sorted) } // Don't forget to free the sumRows array to avoid memory leaks delete[] sumRows;
Full Corrected print Function
Here's the complete print function with all fixes included:
void print(int** arr, int rows, int columns) { // Print original array for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { cout << setw(7) << arr[i][j]; } cout << endl; } // Calculate sum of positive even elements per row int* sumRows = new int[rows]; int sum = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { if (arr[i][j] > 0 && arr[i][j] % 2 == 0) { sum += arr[i][j]; } } cout << "\nSum of positive even elements in the row " << i + 1 << " = " << sum; sumRows[i] = sum; sum = 0; } // Print sum array cout << "\nArray of sums: "; for (int i = 0; i < rows; i++) { cout << setw(7) << sumRows[i]; } cout << endl; // Fixed bubble sort to sort rows by sum (ascending) bool swapped; for (int i = 0; i < rows - 1; i++) { swapped = false; for (int k = 0; k < rows - i - 1; k++) { if (sumRows[k] > sumRows[k + 1]) { swap(arr[k], arr[k + 1]); swap(sumRows[k], sumRows[k + 1]); swapped = true; } } if (!swapped) break; } // Print sorted array cout << "\n\nSwapped array:" << endl; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { cout << setw(7) << arr[i][j]; } cout << endl; } // Clean up allocated memory for sumRows delete[] sumRows; }
Why This Works
- Swapping Entire Rows: Since
arris an array of pointers (each pointing to a row), swappingarr[k]andarr[k+1]instantly reorders the rows without needing to copy every element. - Aligned Sums: Swapping entries in
sumRowsensures the sums always match the current row order. - Early Termination: The
if (!swapped) breakline stops the sort early if the array becomes sorted before all iterations, making it more efficient. - Memory Cleanup: Adding
delete[] sumRowsfixes the memory leak from the dynamically allocated sum array.
This should now correctly sort your 2D array's rows in ascending order based on the sum of positive even elements in each row, while keeping your requirement of using bubble sort and dynamic arrays intact.
内容的提问来源于stack exchange,提问作者ovvvol

