Java 使用多维数组插入排序
在我的代码中,我在第一行接收到值n和d。N是我要写入的值的数量,d是每个位置N中的数字数量。 在接下来的n值中,我引入了d值。本练习的重点是使用插入排序,但如果第一个坐标相等,则会比较第二个坐标,如果再次出现这种情况,则会比较第三个坐标,依此类推。例子: 输入: 输出:Java 使用多维数组插入排序,java,multidimensional-array,insertion-sort,Java,Multidimensional Array,Insertion Sort,在我的代码中,我在第一行接收到值n和d。N是我要写入的值的数量,d是每个位置N中的数字数量。 在接下来的n值中,我引入了d值。本练习的重点是使用插入排序,但如果第一个坐标相等,则会比较第二个坐标,如果再次出现这种情况,则会比较第三个坐标,依此类推。例子: 输入: 输出: 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 =
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;
}
}
我为您找到了递归解决方案,使用下面的代码,您可以按二进制排序格式对多维二进制数组进行排序(我在这里使用常量数组,您可以添加扫描仪以从控制台获取输入):
publicstaticvoidmain(字符串[]args)
{
int n,d;
int tab[][]=newint[][]{{1,1,1,1},{1,0,1,1},{1,1,0,0},{0,0,1},
{ 0, 1, 0, 1 }, { 0, 0, 0, 1 } };
n=6;
d=4;
System.out.println(“--”);
对于(int u=0;u制表符[k][列])
{
for(int l=列;l2)
{
for(int i=rowsStart;i
打印阵列的代码在哪里?我看不出来。我现在就说,我会编辑。你知道怎么了吗?问题是,如果他们不需要改变的话,到最后还是会改变的。但是我也试着提出了一个条件,但是如果一个位置改变了,第二个位置不需要改变,那么第一个位置就不会改变,因为第二个位置不需要改变,如果我提出了这个条件,它也不会做最后的决定。有什么帮助吗?
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 }, { 1, 0, 1, 1 }, { 1, 1, 0, 0 }, { 0, 0, 0, 1 },
{ 0, 1, 0, 1 }, { 0, 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, 0, 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, rowsStart, rowEnd, column + 1);
}
}