简体   繁体   English

如何使用冒泡排序对2D数组进行排序?

[英]How to sort a 2D array using bubble sort?

I need to sort a 2D array in descending order by row using bubble sort based on the last column but I'm having some trouble with it. 我需要基于最后一列使用冒泡排序按行降序对2D数组进行排序,但是我遇到了一些麻烦。

This is the data that I need to arrange in descending order but only based on the last column. 这是我需要按降序排列的数据,但仅基于最后一列。

6814.00      85.00      86.00      92.00      88.00      87.75
7234.00      76.00      81.00      84.00      78.00      79.75
6465.00      87.00      54.00      68.00      72.00      70.25
7899.00      92.00      90.00      88.00      86.00      89.00
9901.00      45.00      78.00      79.00      80.00      70.50
8234.00      77.00      87.00      84.00      98.00      86.50
7934.00      76.00      91.00      84.00      65.00      79.00
7284.00      56.00      81.00      87.00      98.00      80.50
7654.00      76.00      87.00      84.00      88.00      83.75
3534.00      86.00      81.00      84.00      73.00      81.00

This is what I have so far. 到目前为止,这就是我所拥有的。

for(int i = 0; i < 10; i++)
  {
    for(int j = 0; j < 6; j++)
    {
      if(arr1[i][5] < arr1[i+1][5])
      {
        int temp = arr1[i][j];
        arr1[i][j] = arr1[i+1][j];
        arr1[i+1][j] = temp;
      }
    }
  }

But this is what I get, which clearly isn't working. 但这就是我得到的,显然是行不通的。

    6814.00      85.00      86.00      92.00      88.00      87.75
    7234.00      76.00      81.00      84.00      78.00      79.75
    7899.00      92.00      90.00      88.00      86.00      89.00
    9901.00      45.00      78.00      79.00      80.00      70.50
    8234.00      77.00      87.00      84.00      98.00      86.50
    7934.00      76.00      91.00      84.00      65.00      79.00
    7284.00      56.00      81.00      87.00      98.00      80.50
    7654.00      76.00      87.00      84.00      88.00      83.75
    3534.00      86.00      81.00      84.00      73.00      81.00
    6465.00      87.00      54.00      68.00      72.00      70.00   

I also noticed that some of the numbers in the last column got rounded up and I'm not sure why. 我还注意到上一栏中的一些数字被四舍五入,我不确定为什么。 I appreciate any help I can get. 感谢您能提供的任何帮助。

If you only care about the order of the last column, just check the last column, then move the entire row accordingly: 如果您只关心最后一列的顺序,只需检查最后一列,然后相应地移动整行:

void BubbleSort2D(double arr1[][]){
    for(int m=0; m<10; m++) {
        for(int i = 0; i < 9; i++)
          {
              if(arr1[i][5] < arr1[i+1][5])
              {
                for(int j = 0; j < 6; j++)
                {
                  double temp = arr1[i][j];
                  arr1[i][j] = arr1[i+1][j];
                  arr1[i+1][j] = temp;
                }
              }
          }
    }
}

public static void main(String args[]) {
    double[][] arr2D = { {6814.00,      85.00,      86.00,      92.00,      88.00,      87.75},
            {7234.00,      76.00,      81.00,      84.00,      78.00,      79.75},
            {6465.00 ,     87.00,      54.00,      68.00,      72.00,      70.25},
            {7899.00,      92.00,      90.00,      88.00,      86.00,      89.00},
            {9901.00,      45.00,      78.00,      79.00,      80.00,      70.50},
            {8234.00,      77.00,      87.00,      84.00,      98.00,      86.50},
            {7934.00,      76.00,      91.00,      84.00,      65.00,      79.00},
            {7284.00,      56.00,      81.00,      87.00,      98.00,      80.50},
            {7654.00,      76.00,      87.00,      84.00,      88.00,      83.75},
            {3534.00,      86.00,      81.00,      84.00,      73.00,      81.00} };
    driver.BubbleSort2D(arr2D);
    for(int m=0; m<9; m++) {
        for(int i = 0; i < 6; i++)
          {
            System.out.print(arr2D[m][i] + "  ");
          }
        System.out.println();
          }
}

Output: 输出:

7899.0 92.0 90.0 88.0 86.0 89.0 7899.0 92.0 90.0 88.0 86.0 89.0
6814.0 85.0 86.0 92.0 88.0 87.75 6814.0 85.0 86.0 92.0 88.0 87.75
8234.0 77.0 87.0 84.0 98.0 86.5 8234.0 77.0 87.0 84.0 98.0 86.5
7654.0 76.0 87.0 84.0 88.0 83.75 7654.0 76.0 87.0 84.0 88.0 83.75
3534.0 86.0 81.0 84.0 73.0 81.0 3534.0 86.0 81.0 84.0 73.0 81.0
7284.0 56.0 81.0 87.0 98.0 80.5 7284.0 56.0 81.0 87.0 98.0 80.5
7234.0 76.0 81.0 84.0 78.0 79.75 7234.0 76.0 81.0 84.0 78.0 79.75
7934.0 76.0 91.0 84.0 65.0 79.0 7934.0 76.0 91.0 84.0 65.0 79.0
9901.0 45.0 78.0 79.0 80.0 70.5 9901.0 45.0 78.0 79.0 80.0 70.5

The most simple way is you can take a whole row at a time and then sort it. 最简单的方法是一次可以整行进行排序。

for(int i=0;i<10;i++)
{
    int array[6];
    for(int j=0;j<6;j++)
    {
         array[j] = arr1[i][j];
    }
    for(i=0; i<(n-1); i++)
    {
        for(j=0; j<(n-i-1); j++)
        {
             if(array[j]>array[j+1])
             {
                 temp=array[j];
                 array[j]=array[j+1];
                 array[j+1]=temp;
             }
        }
    }
    for(int j=0;j<6;j++)
    {
         arr1[i][j] = array[j];
    }
}

Check this. 检查一下。

for(int i = 1; i <=9; i++)
      {
        for(int j = 9; i <= j; j--) //edit.
        {
          if(arr1[j][5] > arr1[j-1][5])
          {
            for(int k = 0; k <= 5; k++)
            {               
                int temp = arr1[j][k];
                arr1[j][k] = arr1[j-1][k];
                arr1[j-1][k] = temp;    
            }
          }
        }
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM