简体   繁体   中英

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.

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
6814.0 85.0 86.0 92.0 88.0 87.75
8234.0 77.0 87.0 84.0 98.0 86.5
7654.0 76.0 87.0 84.0 88.0 83.75
3534.0 86.0 81.0 84.0 73.0 81.0
7284.0 56.0 81.0 87.0 98.0 80.5
7234.0 76.0 81.0 84.0 78.0 79.75
7934.0 76.0 91.0 84.0 65.0 79.0
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;    
            }
          }
        }
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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