【问题标题】:Sorting a nX3 matrix (Most efficient way)对 nX3 矩阵进行排序(最有效的方法)
【发布时间】:2017-05-04 15:40:18
【问题描述】:

我正在为 Mo 的算法编写代码并在 JAVA 中进行更新,因此我们必须以这种方式对 N X 3 矩阵的 ArrayList 进行升序排序

ArrayList 的格式为

Ai,Bi,Ci

例如:

1,2,3
2,3,4
1,2,1
2,3,5

所以要对它们进行排序,首先查找所有Ai,如果在Ai中存在1,2,3和1,2,1的冲突,则查找Bi,如果存在冲突,则查找Ci,并相应地对其进行排序。

Sorted array
1,2,1
1,2,3
2,3,4
2,3,5

那么有没有什么数据结构可以在不用写一大段代码的情况下对它们进行排序呢?

那些在 cmets 中要求我的代码的人-->

// RIGHT NOW I HAVEN'T MADE AN N X # MATRIX ,WHAT I HAVE DONE IS TAKEN 3 ARRAYLISTS , yes I WILL CHANGE IT INTO A SINGLE ARRAYLIST OF SIZE N X 3
public class MoSAlgorithmUpdates {

public static void main(String[] args) throws IOException {
    //finding no of distict numbers
    BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
    String str;
    //Taking the Array Now
    str=br.readLine();
    ArrayList<Integer> arr=new ArrayList<Integer>();
    StringTokenizer st = new StringTokenizer(str," ");
    while(st.hasMoreTokens())
    {
        arr.add(Integer.parseInt(st.nextToken()));
    }

    // Total q queries including updates + finding distinct numbers

    int Q;
    str = br.readLine();
    Q = Integer.parseInt(str);
    int[] q1=new int[Q];
    int[] q2=new int[Q];
    int[] q3=new int[Q];
    int[] q4=new int[Q];
    ArrayList<Integer> query1=new ArrayList<Integer>();// RIGHT NOW I HAVEN'T MADE AN N X # MATRIX ,WHAT I HAVE DONE IS TAKEN 3 ARRAYLISTS , yes I WILL CHANGE IT INTO A SINGLE ARRAYLIST OF SIZE N X 3
    ArrayList<Integer> query2=new ArrayList<Integer>();// RIGHT NOW I HAVEN'T MADE AN N X # MATRIX ,WHAT I HAVE DONE IS TAKEN 3 ARRAYLISTS , yes I WILL CHANGE IT INTO A SINGLE ARRAYLIST OF SIZE N X 3
    ArrayList<Integer> query3=new ArrayList<Integer>();// RIGHT NOW I HAVEN'T MADE AN N X # MATRIX ,WHAT I HAVE DONE IS TAKEN 3 ARRAYLISTS , yes I WILL CHANGE IT INTO A SINGLE ARRAYLIST OF SIZE N X 3
    ArrayList<Integer> update1=new ArrayList<Integer>();
    ArrayList<Integer> update2=new ArrayList<Integer>();
    ArrayList<Integer> update3=new ArrayList<Integer>();
    //int[] update1=new int[Q];
    int noofupdates=0;
    //noofupdates=time

    for(int i2=0;i2<Q;i2++)
    {
        str=br.readLine();
        StringTokenizer st2 = new StringTokenizer(str," ");

        q1[i2]=Integer.parseInt(st2.nextToken());

        q2[i2]=Integer.parseInt(st2.nextToken());
        q3[i2]=Integer.parseInt(st2.nextToken());

        if(q1[i2]==1)
        {
            query1.add(q2[i2]);
            query2.add(q3[i2]);
            query3.add(noofupdates);
        }
        else
        {
            update1.add(q2[i2]);
            update2.add(q3[i2]);
            noofupdates++;
        }
    }
  }
}

上次查询-->

在Mo's Algorithm with updates,我们排列ArrayList by

int sqrt = Math.sqrt(N) // N is the length of ArrayList containing all the 
                        // numbers from which range queries have to be told 

现在对 .数字为

现在让我们说 L、R、时间用于例如

1,3,3
2,3,4
2,1,1
2,3,5

所以按 L,R,time 对它们进行排序返回结果为

Sorted array
1,3,3
2,1,1
2,3,4
2,3,5

现在必须对它们进行排序,怎么做?最终输出必须像没有平方根一样,即值 L,R,time 不应该改变,但排序必须用 .如何实现——?

根据 N 的值,最终输出可能如下所示

2,1,1
1,3,3 //Very Imp see this it is sorted
2,3,4
2,3,5

【问题讨论】:

  • 大概您可以将列连接成一个列表并通过现有方法对其进行排序?
  • 你的数据结构是ArrayList还是array?请给我们一些相关的代码。
  • ArrayList 是我的数据结构,所以我想使用 Collections 中的 Any 内置方法
  • 请向我们展示您的代码
  • @ patrick-hainge letz 说我有 1,12,3,我有 11,2,3,那么如果我将它们组合起来,我将如何对它们进行排序,它们都相同,即 1123,如何我知道哪个数字是哪个?

标签: java sorting arraylist collections


【解决方案1】:

基数排序 https://en.wikipedia.org/wiki/Radix_sort

您可以使用集合的默认排序方法

Collections.sort(yourArrayN2, comparator);

您应该定义比较器,从第一个元素开始比较两个数组(我们假设这些数组/列表具有相同的大小;否则您将修改比较器)。

比较器:

    public class ListComparator implements Comparator<List<Integer>> {

        @Override
        public int compare(List<Integer> list1, List<Integer> list2) {
            if (list1 == null || list2 == null || list1 == list2) {
                throw new IllegalArgumentException();
            }

            int size = list1.size();
            if (size != list2.size()) {
                throw new IllegalArgumentException();
            }

            for (int i = 0; i < size; i++) {
                int delta = list1.get(i) - list2.get(i);
                if (delta != 0) {
                    return delta;
                }
            }

            return 0;
        }
    }

及其用法

public class App {

    public void execute() {
        List<List<Integer>> list2D = make2DList();
        printList2D(list2D);
        System.out.println("\n");
        Collections.sort(list2D, new ListComparator());
        printList2D(list2D);
    }

    //This is where you create your 2D list. It can be read from file, etc.
    private List<List<Integer>> make2DList() {
        List<List<Integer>> res = new ArrayList<>(3);
        res.add(makeList(1,2,3));
        res.add(makeList(2,3,4));
        res.add(makeList(1,2,1));
        res.add(makeList(2,3,5));
        return res;
    }

    private List<Integer> makeList(Integer ... numbers) {
        List<Integer> res = new ArrayList<>();
        for (Integer i : numbers) {
            res.add(i);
        }
        return res;
    }

    private void printList2D(List<List<Integer>> list2D) {
        for (List<Integer> list : list2D) {
            printList(list);
        }
    }

    private void printList(List<Integer> list) {
        int size = list.size();
        int lastIndex = size - 1;
        for (int i = 0; i < lastIndex; i++) {
            System.out.print(list.get(i) + ", ");
        }
        System.out.print(list.get(lastIndex) + "\n");
    }
}

打印出来

1, 2, 3
2, 3, 4
1, 2, 1
2, 3, 5


1, 2, 1
1, 2, 3
2, 3, 4
2, 3, 5

【讨论】:

  • 你用过一个 ArrayList 还是 3 个 ArrayList?我对任何一个都很酷,但我需要知道你用过什么
  • @ErrorrrDetector yourArrayN2 是包含列表的二维(2 维)列表。 list1,list2 是包含数字的一维列表
  • 你能向我解释一下你在这部分做了什么吗? for (int i = 0; i
  • docs.oracle.com/javase/7/docs/api/java/util/Comparator.html 换句话说,A 0) ... else if (list1[i] - list2[i] > 0) ... else。我只是缩短了这个逻辑: (list1[i] - list2[i]) 重复了两次。
  • 小心。上面的解决方案解决了数字(在我们的例子中是整数)的这个任务。对于其他对象,而不是 int delta = list1.get(i) - list2.get(i); 使用 int delta = list1.get(i) 。 compareTo ( list2.get(i) );
猜你喜欢
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 2021-02-02
  • 1970-01-01
相关资源
最近更新 更多