【问题标题】:Bubble-Sort 2D array冒泡排序二维数组
【发布时间】:2015-12-25 21:11:49
【问题描述】:

我需要构建一个对二维数组进行冒泡排序的代码。这里的诀窍是我不能使用一维数组助手,或者将项目移动到另一个数组。

排序需要在二维数组上。

现在我构建了我的函数。但是出了点问题。这是我的输出

1      1      2      6     12     32
 49     44     54     55    100    344

即将完成,我想不出该怎么做。

 public static int [] [] sortMatrix(int[][]matrix){
    for(int x = matrix.length  ; x >0  ; x-- ){
        for(int i = matrix[0].length  ; i > 0 ; i-- ){
            for(int j = 0 ; j < x  ; j++){
                for(int t = 0 ;t < i    ;  t++){
                    if(t < matrix[0].length - 1 && matrix[j][t] > matrix[j][t+1] ){
                        swap(matrix , j , t, t+1);
                    }
                    else if(t == matrix[0].length - 1 && j != matrix.length -1&& matrix[j][t] > matrix[j+1][0] ){
                        swap1(matrix ,j , t , j + 1);
                    }                       
                }
            }               
        }           
    }

【问题讨论】:

  • 输入预期输出对会有所帮助
  • swapswap1的功能不一样?
  • 不,因为一个交换在行中,另一个在列之间
  • int [][] 矩阵 = {{49, 54, 32,1, 2,6} , {1,55,44,100,344,12}};

标签: java arrays sorting


【解决方案1】:

试试

 public static int [] [] sortMatrix(int[][]matrix){
    // for loop of matrix rows: -
    for(int x = 0  ; x < matrix.length; x++){
        // for loop of matrix columens: -
        for(int i =0; i < matrix[x].length; i++){
            // for loop of comparison and swapping
            for(int t = 0; t < matrix[x].length - i - 1; t++){
                 if(matrix[x][t] > matrix[x][t+1]){
                      // Swapping operation: -
                      int temp = matrix[x][t];
                      matrix[x][t] = matrix[x][t+1];
                      matrix[x][t+1] = temp;
                 }
            }
        }
    }                         
    return matrix;
}

而不是

 public static int [] [] sortMatrix(int[][]matrix){
    for(int x = matrix.length  ; x >0  ; x-- ){
        for(int i = matrix[0].length  ; i > 0 ; i-- ){
            for(int j = 0 ; j < x  ; j++){
                for(int t = 0 ;t < i    ;  t++){
                    if(t < matrix[0].length - 1 && matrix[j][t] > matrix[j][t+1] ){
                        swap(matrix , j , t, t+1);
                    }
                    else if(t == matrix[0].length - 1 && j != matrix.length -1&& matrix[j][t] > matrix[j+1][0] ){
                        swap1(matrix ,j , t , j + 1);
                    }                       
                }
            }               
        }           
    }

【讨论】:

    【解决方案2】:

    下面是对二维数组进行排序的代码,诀窍是你必须将二维数组视为一维数组,然后为索引导出适当的行、偏移对。

    import java.util.Arrays;    
    
    public class Bubble2DSort {
        public static void main(String[] args) {
            System.out.println("Started");
    
            int[][] matrix = {{49,44,54,55,100,344}, {1,1,2,6,12,32}};
    
            sortMatrix(matrix);
    
            System.out.println("Printing output ");
    
            for(int[] rowArr : matrix) {
                System.out.println(Arrays.toString(rowArr));
            }
        }
    
        private static void sortMatrix(int[][] matrix) {
            int row = matrix.length;
            int col = matrix[0].length;
            int totalCount = row * col;
    
            System.out.println("totalCount : " +totalCount);
    
            boolean noSwaps = false;
            for(int i = 0; !noSwaps; i++) {
                noSwaps = true;
    
                for(int j = 1; j < totalCount - i; j++) {
                    int currentRow = (j-1) / col;
                    int currentOffset = (j-1) % col;
                    int nextRow = j / col;
                    int nextOffset = j % col;
    
                    if( matrix[currentRow][currentOffset] > matrix[nextRow][nextOffset]) {
                        //swapping
                        int temp = matrix[nextRow][nextOffset];
                        matrix[nextRow][nextOffset] = matrix[currentRow][currentOffset];
                        matrix[currentRow][currentOffset] = temp;
    
                        noSwaps = false;
                    }
                }
            }
        }
    }
    

    输出:

    Started
    totalCount : 12
    Printing output 
    [1, 1, 2, 6, 12, 32]
    [44, 49, 54, 55, 100, 344]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 2014-06-10
      • 2021-10-31
      • 2020-09-13
      • 2010-12-23
      • 1970-01-01
      相关资源
      最近更新 更多