加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用多维数组的InsertionSort

发布时间:2020-12-15 02:14:35 所属栏目:Java 来源:网络整理
导读:在我的代码中,我在第一行收到值n和d. N将是我想要写入的值的数量,d是每个位置n中的数字的数量. 所以在接下来的n个值中我会介绍d值.这个练习的目的是使用插入排序,但如果第一个坐标是相等的,它会比较第二个,如果再次发生则比较第三个,依此类推.例: 输入: 5
在我的代码中,我在第一行收到值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);
    }
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读