以升序对2d数组排序 [英] Sorting a 2d array in ascending order

查看:47
本文介绍了以升序对2d数组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 {4,2},{1,7},{4,5},{1,2},{1,1},{4,1} 到此 {1,1},{1,2},{1,7},{4,1},{4,2},{4,5} .但是由于某种原因,它仍然给我原始数组并且不排序.我想念什么?

I'm trying to sort this 2d array in ascending order from {4,2},{1,7},{4,5},{1,2},{1,1},{4,1} to this {1,1},{1,2},{1,7},{4,1},{4,2},{4,5}. But for some reason it still gives me the original array and doesnt sort. What am I missing?

public class Sort {
    public static void main(String[] args) {
        int[][] array = {{4, 2}, {1, 7}, {4, 5}, {1, 2}, {1, 1}, {4, 1}};
        sort(array);
        print(array);
    }

    //Row Length
    public static void print(int[][] m) {
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[i].length; j++) {
                System.out.print("{" + m[i][j] + "}" + " ");
            }
            System.out.println();
        }
    }

    public static int sort(int A[][]) {
        int newArray = 0;
        for (int i = 0; i < A.length; i++) {
            for (int j = i + 1; j < A[i].length - 1; j++) {
                if (A[j][0] > A[j + 1][0]) {
                    int temp = 0;
                    temp = A[i][j];
                    A[i][j] = A[i + 1][j + 1];
                    A[i + 1][j + 1] = A[i][j];

                    System.out.print(" " + A[i][j]);
                }
            }
        }
        return newArray;
    }
}

推荐答案

这就是您想要的:

  • 您的if语句从不执行,因为您的逻辑在以下情况下是错误的做比较.

  • Your if statement never executed, because your logic was wrong when doing comparisons.

因此,永远不会在原始代码中设置温度.

Consequently temp never ever got set in your original code.

最重要的是, temp 应该是 int [] ,而不是 int .

To top that off, temp should have been an int[], not an int.

也无需从sort方法返回 int .

There was also no need to return an int from the sort method.

我还稍微调整了您的打印例程.

I also slightly tweaked your print routine.

import static java.lang.System.out;
public class Playground {
    public static void main(String[] args) {
        int[][] array = {{4, 2}, {1, 7}, {4, 5}, {1, 2}, {1, 1}, {4, 1}};
        sort(array);
        print(array);
    }

    public static void print(int[][] m) {
        for (int i = 0; i < m.length; i++) {
            if (i > 0) {
                out.print(",");
            }
            out.print("{" + m[i][0] + "," + m[i][1] + "}");
        }
        out.println();
    }

    public static void sort(int A[][]) {
        boolean unsorted = true;
        while (unsorted) {
            unsorted = false;
            for (int i = 0; i < A.length - 1; i++) {
                if ((A[i][0] > A[i + 1][0])
                        || ((A[i][0] == A[i + 1][0])
                        && (A[i][1] > A[i + 1][1])
                )) {
                    int[] temp = new int[2];
                    temp[0] = A[i][0];
                    temp[1] = A[i][1];
                    A[i][0] = A[i + 1][0];
                    A[i][1] = A[i + 1][1];
                    A[i + 1] = temp;
                    unsorted = true;
                }
            }
        }
    }
}

您可以在此处看到它运行.

这篇关于以升序对2d数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆