Java – insertionsort using multidimensional arrays

In my code, I receive the values n and D on the first line. N will be the number of values I want to write, and D is the number of numbers in each position n

5 3
1 1 1
1 0 1
1 1 0
0 1 1
0 1 0

Output:

0 1 0
0 1 1
1 0 1
1 1 0
1 1 1

This is my code:

public static void main(String[] args) {
    int n,d,aux;
    Scanner sc = new Scanner( system.in );

    n = sc.nextInt();
    d = sc.nextInt();
    int tab [][] = new int[n][d];
    for(int i=0;i<n;i++){
        for(int j=0;j<d;j++){
            aux = sc.nextInt();
            tab[i][j] = aux;
        }
    }
    insertionSort(tab,d);
    System.out.println("---");
    for(int u=0;u<tab.length;u++){
        for(int y=0;y<d;y++){
            System.out.print(tab[u][y]+" ");
        }
        System.out.println();
    }
}
public static void insertionSort(int tab[][],int d){
    int i,j;
    int pos = 0;
    int tmp [][] = new int[1][d];
    for(i = 1;i < tab.length;i++)
    {
        for(int k=0;k<d;k++)
            tmp[0][k] = tab[i][k];

        for(j = i; j>0 && tmp[0][0] <= tab[j-1][0];j--)
        {
            while(tmp[0][pos] == tab[j-1][pos] && pos+1<d){
                pos++;
                if(tmp[0][pos] < tab[j-1][pos]){
                    pos=0;
                    break;
                }
            }
            if(pos==0){
                for(int k=0;k<d;k++)
                    tab[j][k] = tab[j-1][k];
            }
        }
        for(int k=0;k<d;k++)
            tab[j][k] = tmp[0][k];
        pos = 0;
    }
}

The problem is that my output is wrong:

0 1 0
0 1 1
1 1 0
1 1 1
1 1 1

Solution

I have found a recursive solution. The following code can sort multidimensional binary arrays in binary sorting format (I use constant arrays here, and you can add your scanner to get input from the console):

public static void main(String[] args)
  {
    int n,d;

    int tab[][] = new int[][] { { 1,1,1 },{ 1,0 },{ 0,1 } };
    n = 6;
    d = 4;
    System.out.println("---");
    for (int u = 0; u < tab.length; u++)
    {
      for (int y = 0; y < d; y++)
      {
        System.out.print(tab[u][y] + " ");
      }
      System.out.println();
    }

    insertionSort(tab,n);

    System.out.println("---");
    for (int u = 0; u < tab.length; u++)
    {
      for (int y = 0; y < d; y++)
      {
        System.out.print(tab[u][y] + " ");
      }
      System.out.println();
    }
  }

  public static void insertionSort(int tab[][],int n)
  {
    doSort(tab,n,0);
  }

  /**
   * Recurring Method for Insertion Sort in MulitDimentional array.
   * 
   * @param tab mulitidiamentional array.
   * @param rowsStart the rows start index
   * @param rowEnd  the row end index
   * @param column the current column
   */
  public static void doSort(int tab[][],int rowsStart,int rowEnd,int column)
  {
    int totalColumn = tab[0].length;
    for (int j = rowsStart; j < rowEnd; j++)
    {
      for (int k = j + 1; k < rowEnd; k++)
      {
        if (tab[j][column] > tab[k][column])
        {
          for (int l = column; l < totalColumn; L++)
          {
            int t = tab[j][l];
            tab[j][l] = tab[k][l];
            tab[k][l] = t;
          }
        }
      }
    }

    int value = tab[rowsStart][column];
    if (rowEnd - rowsStart > 2)
    {
      for (int i = rowsStart; i < rowEnd; i++)
      {
        if (value != tab[i][column])
        {
          doSort(tab,rowsStart,i,column + 1);
          value = tab[i][column];
          rowsStart = i;
        }
      }
    }
    if (column < totalColumn - 1)
    {
      doSort(tab,rowEnd,column + 1);
    }
  }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>