如果值在第一列上的排序相同,则根据值的差异对多维数组进行排序

时间:2017-12-26 00:00:04

标签: java arrays sorting multidimensional-array time-complexity

如果值相同,则根据值的差异对多维数组进行排序;排在第一列。
需要Java解决方案。 如果不是代码,我想知道解决这类问题的方法或任何类型的提示。

约束: 行中没有任何行可以是固定的列,即2。

Example: 
int arr[][] = new int[5][2]

    [0] [1]
[0]  0   6
[1]  0   7
[2]  4   5
[3]  2   3
[4]  0   1

Final Output:

    [0] [1]
[0]  0   1
[1]  2   3
[2]  4   5
[3]  0   6
[4]  0   7

Explanation:

difference between: arr[0][1] - arr[0][0]  -> 6-0 -> 6
difference between: arr[1][1] - arr[1][0]  -> 7-0 -> 7
difference between: arr[2][1] - arr[2][0]  -> 5-4 -> 1    — same length ie 1
difference between: arr[3][1] - arr[3][0]  -> 3-2 -> 1  —  same length ie 1
difference between: arr[4][1] - arr[4[0]  -> 1-0 -> 1   — same length ie 1

I want to sort on the difference, in cases where difference is same I want to sort those with same difference on column [0]

So in this case, 
The below 3 have same difference
difference between: arr[2][1] - arr[2][0]  -> 5-4 -> 1    — same difference ie 1
difference between: arr[3][1] - arr[3][0]  -> 3-2 -> 1  —  same difference ie 1
difference between: arr[4][1] - arr[4[0]  -> 1-0 -> 1   — same difference ie 1

Need to sort the above 3 based on there column[0] value
arr[2][1] - arr[2][0]  -> 5-4 -> 1    —  value here is 4 ie arr[2][0] 
arr[3][1] - arr[3][0]  -> 3-2 -> 1  — value here is 2 ie arr[3][0]
arr[4][1] - arr[4[0]  -> 1-0 -> 1   — value here is 1 ie arr[4][0]

So the the one with the least value in column[0] should be first,
In final output, 
arr[4][1] - arr[4[0]  -> 1-0 -> 1   ——> 1st
arr[3][1] - arr[3][0]  -> 3-2 -> 1  ——> 2nd
arr[4][1] - arr[4[0]  -> 1-0 -> 1   ——> 3rd 
arr[0][1] - arr[0][0]  -> 6-0 -> 6  ——> 4th
arr[1][1] - arr[1][0]  -> 7-0 -> 7  ——> 5th

编辑1: 确定时间复杂度需要帮助吗? 感谢您的所有建议和代码。 以下是我的工作代码版本: 我想知道我的代码的时间复杂性? 简而言之,排序二维数组的复杂性是多少。 1d阵列 - > O(n.logn) 2d - > ??

private static int solve(int pathLength, int[][] floristIntervals) {
        // TODO Auto-generated method stub
        System.out.println(Arrays.deepToString(floristIntervals));
        Arrays.sort(floristIntervals, new Comparator<int[]>(){

            @Override
            public int compare(int[] o1, int[] o2) {
                // TODO Auto-generated method stub

                /*System.out.println(o1[0]);
                System.out.println(o1[1]);
                System.out.println(o2[0]);
                System.out.println(o2[1]);*/
                if(o2[1]-o2[0] == o1[1]-o1[0]){
                    if(o2[0] > o1[0]){
                        return -1;
                    }
                    return 1;
                }
                if (o2[1]-o2[0] > o1[1]-o1[0])
                    return 1;
                else
                    return -1;
            }

        });
        System.out.println(Arrays.deepToString(floristIntervals));

1 个答案:

答案 0 :(得分:0)

好的,所以这是一个可能的解决方案:

package com.company;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        // creating Scanner and declaring and initializing our 2D array
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number of rows for your 2D array: ");
        int rows = in.nextInt();
        int[][] arr = new int[rows][2];

        // asking user for values to assign to our array
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < 2; j++) {
                System.out.print("Enter value for row " + i + " column " + j + ": ");
                arr[i][j] = in.nextInt();
            }
        }

        // create a Comparator that compares by result of reduction of 2nd and first column in each array, then if the
        // results are equal (0), we compare by first column
        Comparator<int[]> cmp = Comparator.<int[]>comparingInt(x -> x[1] - x[0]).thenComparing(Comparator.comparingInt(x -> x[0]));

        // we make a stream out of the 2D array, we sort inner elements (single dimensional arrays) by the Comparator we created and we print out results
        Arrays.stream(arr)
              .sorted(cmp)
              .forEach(x -> System.out.println(Arrays.toString(x)));

    }

}

输入数组:

    [0] [1]

[0]  0   6

[1]  0   7

[2]  4   5

[3]  2   3

[4]  0   1

输出数组:

[0, 1]
[2, 3]
[4, 5]
[0, 6]
[0, 7]

询问您是否有更多问题。