【问题标题】:How to sort two or more arrays using single array? [duplicate]如何使用单个数组对两个或多个数组进行排序? [复制]
【发布时间】:2021-11-23 14:38:04
【问题描述】:

如何通过只对一个数组进行排序来对三个数组进行排序?

int arr[]={20,10,5,22};
int arr2[]={120,344,43,122};
int arr3[]={2234,12,23,3434};

我想要的是对第一个数组arr进行排序,其余数组必须根据第一个数组的索引进行排序。

output:
  5 10 20 22

如上arr 之前排序索引 5 为 3 现在为 0。类似地索引 43 和 12 应更改为 0

排序后的预期数组将是

5 10 20 22

43 344 120 122

23 12 2234 3424  

当我们在冒泡排序或任何其他交换中交换元素时,我想要什么,我们通过交换更改为元素的索引。我想要类似于其他两个数组。

swap(arr[0],arr[2])

swap(arr1[0],arr1[2])

swap(arr2[0],arr2[2])

如果我实现自定义排序但想要减少代码的东西,这是可能的。

【问题讨论】:

  • 不,这不能回答我的问题
  • 你做了什么吗?请向我们展示您的代码。
  • 你自己尝试过什么?你通过首先思考一个算法,然后实现它来学习编程。来到这里并要求人们为你做这些......意味着你减慢你的学习速度。你得到的那个链接给了你前半部分:你需要了解第一个数组中 INDEXES 的顺序,然后将其应用于其他数组。
  • 当然它确实回答了你的问题:你找到了排列,然后使用这个排列对所有三个数组重新排序。另一种方法是使用自定义的“移动数组元素”函数,但 Java 中没有任何内置功能:您需要使用库或自己实现排序。

标签: java


【解决方案1】:
  1. 在第一个arr数组的基础上构建索引列表。
  2. 根据索引对剩余的每个数组进行排序。
static int[] sort(List<Integer> indexes, int[] arr) {
    return indexes.stream().mapToInt(i -> arr[i]).toArray();
}

public static void main(String[] args) {
int arr[]={20,10,5,22};
    int arr2[]={120,344,43,122};
    int arr3[]={2234,12,23,3434};
    
    List<Integer> indexes = IntStream.range(0, arr.length)
                            .boxed()
                            .sorted(Comparator.comparingInt(i -> arr[i]))
                            .collect(Collectors.toList());
    System.out.println(indexes);
    
    Arrays.sort(arr);
    arr2 = sort(indexes, arr2);
    arr3 = sort(indexes, arr3);
    
    System.out.println(Arrays.toString(arr));
    System.out.println(Arrays.toString(arr2));
    System.out.println(Arrays.toString(arr3));
}

输出:

[2, 1, 0, 3]
[5, 10, 20, 22]
[43, 344, 120, 122]
[23, 12, 2234, 3434]

【讨论】:

    【解决方案2】:

    您可以这样做。这只是一个示例,性能可能会很低。

    选项 1. Lambda

    public static void main(String... args) {
        int[] arr = { 20, 10, 5, 22 };
        int[] arr2 = { 120, 344, 43, 122 };
        int[] arr3 = { 2234, 12, 23, 3434 };
    
        sort(arr2, arr);
        sort(arr3, arr);
        Arrays.sort(arr);
    }
    
    private static void sort(int[] arr, int[] indices) {
        final class Pair {
    
            private final int index;
            private final int value;
    
            public Pair(int index, int value) {
                this.index = index;
                this.value = value;
            }
    
        }
    
        AtomicInteger pos = new AtomicInteger(0);
    
        IntStream.range(0, arr.length)
                 .mapToObj(i -> new Pair(indices[i], arr[i]))
                 .sorted(Comparator.comparingInt(one -> one.index))
                 .map(pair -> pair.value)
                 .forEach(value -> arr[pos.getAndIncrement()] = value);
    }
    

    选项 2. PriorityQueue

    private static void sort(int[] arr, int[] indices) {
        final class Pair {
    
            private final int index;
            private final int value;
    
            public Pair(int index, int value) {
                this.index = index;
                this.value = value;
            }
    
        }
    
        Queue<Pair> queue = new PriorityQueue<>(Comparator.comparingInt(one -> one.index));
    
        for (int i = 0; i < arr.length; i++)
            queue.add(new Pair(indices[i], arr[i]));
    
        int i = 0;
    
        while (!queue.isEmpty())
            arr[i++] = queue.remove().value;
    }
    

    选项 3. TreeMap

    private static void sort(int[] arr, int[] indices) {
        Map<Integer, List<Integer>> map = new TreeMap<>();
    
        for (int i = 0; i < arr.length; i++) {
            if (!map.containsKey(indices[i]))
                map.put(indices[i], new ArrayList<>());
            map.get(indices[i]).add(arr[i]);
        }
    
        int i = 0;
    
        for (List<Integer> values : map.values())
            for (int value : values)
                arr[i++] = value;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多