微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 使用多维数组的InsertionSort

在我的代码中,我在第一行收到值n和d. N将是我想要写入的值的数量,d是每个位置n中的数字的数量.
所以在接下来的n个值中我会介绍d值.这个练习的目的是使用插入排序,但如果第一个坐标是相等的,它会比较第二个,如果再次发生则比较第三个,依此类推.例:
输入:

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

输出

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

这是我的代码

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;
    }
}

问题是我的输出错了:

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

解决方法

我找到了一个递归的解决方案,下面的代码可以用二进制排序格式对多维二进制数组进行排序(我在这里使用常量数组,你可以添加你的扫描程序来从控制台获取输入):

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);
    }
  }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐