如何使用單個數組對兩個或多個數組進行排序?

disamaj964

如何通過僅對單個數組進行排序來對三個數組進行排序?

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])

如果我實現自定義排序但想要一些可以減少代碼的東西,這是可能的。

奧列格切雷德尼克

這就是你如何做到這一點。這只是一個例子,性能可能很低。

選項 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. 樹形圖

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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何對兩個單獨數組的兩個值執行函數

如何使用冒泡排序按數組長度對二維數組或多個數組進行排序

如何使用 sequelize 和 node 插入由多個對象組成的數組?

根據各個行對 3D 數組中的 2D 數組列進行單獨排序

如何從兩個不同長度的數組javascript創建對像數組

PHP中根據兩個條件對多個關聯數組進行分組和排序

按另一個對像數組對對像數組進行排序

如何使用 Array.prototype.sort() 按兩個屬性對數組進行排序?

如何使用兩個參數對元組進行排序?

同時對兩個數組進行排序

根據最佳匹配另一個數組對對像數組進行排序

如何根據javascript中的第一個元素對多個嵌套數組的第二個元素進行分組?

如何對同一個數組元素進行兩次操作?

如何使用 map() 函數對列表中的單個單詞進行排序?

按字符串php對多個數組進行排序

按第一個字母對數組進行分組 - 使用 groupBy 方法 (Lodash)

使用月對兩個對像數組進行聚類

如何在單獨的數組中計算數組中的每個對象?

如何在不使用排序方法(排序)或排序算法(冒泡排序、快速排序)的情況下對兩個已排序數組進行排序

如何根據基於不同數組的具有 2 個屬性的對像對數組進行排序

如何通過對多個變量進行分組來創建新的 Pandas 數據框?

在 Pandas DataFrame 中,當每個單元格都是一個數組時,對每個子數組進行排序

如何在按另一個鍵排序之前按鍵對 PHP 數組進行分組?

使用 Array.reduce() 按多個鍵對數組進行分組

如何在兩個對像數組之間進行更改?[洛達什/JS]

C# 按多個不同條件對數組進行排序

排序數組,裡面有兩個對象

如何按多個日期字段對對像數組進行排序?

用C中的冒泡排序通過多個條件對結構數組進行排序