You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java作业:用辅助方法对二维int数组行和列排序(无Comparator)

Solving 2D Array Column Sorting with Two Different Methods

Hey there! I see you've already nailed a bubble sort implementation for sorting rows in your 2D int array—great start! Let's work through the column sorting bottleneck and give you two distinct sorting methods to meet your assignment's requirements.

Method 1: Column-Focused Bubble Sort (In-Place)

This builds directly on the bubble sort logic you already wrote, but adjusts the iteration to target columns instead of rows. Since columns are vertical slices of the 2D array, we'll loop through each column index, then compare and swap elements vertically across rows.

public static void sortOneColumnBubble(int[][] arr2D, int columnIndex) {
    // First, validate the column index is within bounds
    if (columnIndex < 0 || columnIndex >= arr2D[0].length) {
        throw new IllegalArgumentException("Invalid column index!");
    }
    
    int rowCount = arr2D.length;
    int temp;
    
    // Similar bubble sort structure, but iterate over rows for the target column
    for (int i = 0; i < rowCount; i++) {
        for (int j = i + 1; j < rowCount; j++) {
            // Compare elements in the same column across different rows
            if (arr2D[i][columnIndex] > arr2D[j][columnIndex]) {
                temp = arr2D[i][columnIndex];
                arr2D[i][columnIndex] = arr2D[j][columnIndex];
                arr2D[j][columnIndex] = temp;
            }
        }
    }
}

How this works:

  • We first check if the column index is valid to avoid out-of-bounds errors (a good practice for robustness)
  • Instead of iterating over elements in a single row, we iterate over rows for the specified column, swapping elements just like your row bubble sort does.

Method 2: Selection Sort with Auxiliary Array

For the second distinct method, let's use selection sort (a different algorithm from bubble sort) with an auxiliary array. This approach extracts the column into a 1D array, sorts it with selection sort, then copies the sorted values back into the original 2D array's column.

First, here's the selection sort helper for a 1D array:

public static void selectionSort(int[] arr) {
    int n = arr.length;
    for (int i = 0; i < n - 1; i++) {
        // Find the index of the minimum element in unsorted part
        int minIndex = i;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }
        // Swap the found minimum element with the first element of unsorted part
        int temp = arr[minIndex];
        arr[minIndex] = arr[i];
        arr[i] = temp;
    }
}

Now the column sorting method using this:

public static void sortOneColumnSelection(int[][] arr2D, int columnIndex) {
    if (columnIndex < 0 || columnIndex >= arr2D[0].length) {
        throw new IllegalArgumentException("Invalid column index!");
    }
    
    int rowCount = arr2D.length;
    int[] columnArr = new int[rowCount];
    
    // Extract the column into a 1D auxiliary array
    for (int i = 0; i < rowCount; i++) {
        columnArr[i] = arr2D[i][columnIndex];
    }
    
    // Sort the auxiliary array with selection sort (different from your bubble sort)
    selectionSort(columnArr);
    
    // Copy the sorted values back into the original 2D array's column
    for (int i = 0; i < rowCount; i++) {
        arr2D[i][columnIndex] = columnArr[i];
    }
}

How this works:

  • We pull the entire column into a separate 1D array—this makes it easier to apply a standard selection sort
  • Selection sort works by repeatedly finding the minimum element in the unsorted portion and swapping it into place, which is a distinct algorithm from your bubble sort (bubbling larger elements to the end vs. selecting minima)
  • Finally, we write the sorted values back to the original column.

Quick Example Usage

You can call these methods like this to test them out:

public static void main(String[] args) {
    int[][] myArray = {{3, 1, 4}, {1, 5, 9}, {2, 6, 5}};
    
    // Sort column 1 (0-indexed) with bubble sort method
    sortOneColumnBubble(myArray, 1);
    
    // Sort column 2 with selection sort method
    sortOneColumnSelection(myArray, 2);
}

Now you have two distinct sorting algorithms (bubble sort and selection sort) applied to columns, which meets your assignment's requirements. Let me know if you need clarification on any part!

内容的提问来源于stack exchange,提问作者samdukez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:13:31